Home > Software design >  C 17: Deducing function noexcept specifier as non-type parameter
C 17: Deducing function noexcept specifier as non-type parameter

Time:06-02

I've noticed that MSVC sometimes fails to deduce non-type parameters that other compilers accept, and recently came upon a simple example involving the function noexcept specifier (which is part of the function's signature since C 17):

template <typename T> struct is_nocv_method : public std::false_type { };

template <typename ReturnT, typename ClassT, bool IsNoexcept, typename... Args>
struct is_nocv_method<ReturnT (ClassT::*)(Args...) noexcept(IsNoexcept)> : std::true_type { };

Godbolt suggests gcc 12.1 and clang 14.0 accept this without issue, but MSVC 14.31 (cl.exe 19.31) fails to compile, claiming IsNoexcept cannot be deduced. Is this a compiler defect?

Demo

CodePudding user response:

A non-type template parameter cannot be deduced from a noexcept-specifier.

[temp.deduct.type]/8 gives the list of contexts from which template parameters can be deduced. Essentially, it can be read as a list of ways to "unwrap" the argument type, and a list of positions in the unwrapped type from which template arguments can be deduced.

For example, the item T (T::*)(T) implies that if the parameter type and the argument type are both pointers to member functions, then template parameters can be deduced from the return type, the class type, and any argument types (for the member function), should they appear there.

You'll notice that there is no item of the form T() noexcept(i), T(T) noexcept(i), and so on.

Yet, some compilers choose to allow this kind of deduction anyway, probably because it is convenient. I would support adding it to the standard.

CodePudding user response:

See this bug report: https://developercommunity.visualstudio.com/t/noexcept-is-not-deducible-in-partial-spe/841571

And this CWG issue: https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#2355

It appears that deduction from noexcept specifiers is indeed nonstandard (but likely an oversight).

  • Related