On C++ Primer on noexcept
exception specification, it is said that a pointer to a function that may throw Implicitly (defined without exception specification e.g: void(*p)();
) or explicitly (void(*p)() noexcept(false);
) may point to any function even to a non-throwing function.
On the other hand a function pointer which may not throw (noexcept
e.g void(*p) noexcept;
) can only point to a function that won't throw.
I found that very logical because the first pointer it is OK to point to a non-throwing function from a throwing function pointer and the second too is so logical.
I've tried this to understand more:
void func1(){ // may throw
std::cout << "func1()\n";
}
void func2() noexcept(false){ // may throw
std::cout << "func2()\n";
}
void func3() noexcept(true){ // won't throw
std::cout << "func3()\n";
}
void func4() noexcept{ // won't throw
std::cout << "func4()\n";
}
int main(int argc, char* argv[]){
void(*pFn1)();
pFn1 = func1; // OK
pFn1 = func2; // OK
pFn1 = func3; // OK
pFn1 = func4; // OK
void(*pFn2)() noexcept(false);
pFn2 = func1; // OK
pFn2 = func2; // OK
pFn2 = func3; // OK
pFn2 = func4; // OK
void(*pFn3)() noexcept(true);
pFn3 = func1; // Error on C++ 17 and above. OK on C++11 and 14
pFn3 = func2; // Error on C++ 17 and above. OK on C++11 and 14
pFn3 = func3; // OK
pFn3 = func4; // OK
void(*pFn4)() noexcept(true);
pFn4 = func1; // Error on C++ 17 and above. OK on C++11 and 14
pFn4 = func2; // Error on C++ 17 and above. OK on C++11 and 14
pFn4 = func3; // OK
pFn4 = func4; // OK
std::cout << '\n';
}
- When I compile the program against
-std=c++17
,-std=c++2a
it works as it should so I get the errors as I've written in the lines comments. But when I compile against-std=c++11
,-std=c++14
I get them all work and the compiler doesn't complain?!
Does this mean the standard has changed? Thank you!