I have read the book C++ Primer 5th ed. In section Exception Specifications and Pointers, Virtuals, and Copy Control, it says:
That is, if we declare a pointer that has a nonthrowing exception specification, we can use that pointer only to point to similarly qualified functions.
And I also refer to noexcept specifier (since C++11), there is also something similar:
Pointers to non-throwing functions are implicitly convertible (since C++17)can be assigned (until C++17) to pointers to potentially-throwing functions, but not the other way around.
void ft(); // potentially-throwing
void (*fn)() noexcept = ft; // error
When I compile the sample snippet with gcc version 5.4.0 20160609
, there is no error or warning.
But when I compile it with Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
, it complains that error C2440: 'initializing': cannot convert from 'void (__cdecl *)(void)' to 'void (__cdecl *)(void) noexcept'
. It seems right behavior.
So I wonder is there something wrong with gcc
compiler?