I realized that I can declare a pointer to noexcept function via using, but I am forbidden such declaration if I use a typedef. Consider the code below:
#include <iostream>
using fptr = void(*)() noexcept;
// typedef void(*FPTR)() noexcept; // fails to compile
void f() noexcept
{
std::cout << "void f() noexcept" << std::endl;
}
void g()
{
std::cout << "void g()" << std::endl;
throw 10;
}
int main()
{
fptr f1 = f;
fptr f2 = g; // why can we do this?
try {
f1();
f2();
}
catch (...)
{
std::cout << "Exception caught" << std::endl;
}
}
If I uncomment the FPTR declaration, I'm getting
error: 'FPTR' declared with an exception specification
However, the using works just fine. My questions are:
- Why this inconsistency?
- Why can we even use
usingwithnoexceptsince we can bind the pointer to functions not declarednoexcept, as in the linefptr f2 = g;
Aucun commentaire:
Enregistrer un commentaire