AnArrayOfFunctions AnArrayOfFunctions - 5 months ago 24
C++ Question

Why isn't the "noexcept" specifier part of the function type?

I don't get it why? I don't think compatibility should be a problem as functions declared without the specifier actually have it implicitly defined to false. If it's about name mangling - can we just suppose that old one (existing) will imply noexcept(false) and add another new symbol to the mangling for noexcept(true).

This is going to be useful when working with templates as now comparing function type and noexcept specifier should be done seperatly. What I basically mean is this:

int func() noexcept(true), func_1() noexcept(false);

decltype(func) == decltype(func_1); //this now equals true

But on the other hand if we had function assignment by either using pointer or reference then - the noexcept specifier is checked as if it was part of the type:

int (&refFunc)() noexcept(true) = func_1; //target exception specification is not superset of source

int (&refFunc)() noexcept(true) = func; //ok

So for now implementing full function matching should be done by both performing type and noexcept check which is kinda complex:

decltype(func) == decltype(func_1) && noexcept(func()) == noexcept(func_1()); //this now equals false

Imagine if functions have got parameters:

int func(int, double) noexcept(true), func_1(int, double) noexcept(false);

decltype(func) == decltype(func_1) && noexcept(func(int{}, double{})) == noexcept(func_1(int{}, double{})); //this now equals false

Answer Source

Starting with C++17, the noexcept specifier is a part of the function type.


The noexcept-specification is a part of the function type and may appear as part of any function declarator. (since C++17)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download