mardi 10 mars 2015

noexcept function pointers aliases vs typedefs inconsistency

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:



  1. Why this inconsistency?

  2. Why can we even use using with noexcept since we can bind the pointer to functions not declared noexcept, as in the line fptr f2 = g;


Aucun commentaire:

Enregistrer un commentaire