I think the following code is well-formed:
template< typename T >
using IsSigned = std::enable_if_t< std::is_signed_v< T > >;
template< typename T, IsSigned< T >... >
T myAbs( T val );
Others say that it is ill-formed, because §17.7 (8.3) of the C++17 standard:
Knowing which names are type names allows the syntax of every template to be checked. The program is ill-formed, no diagnostic required, if: (...) every valid specialization of a variadic template requires an empty template parameter pack, or (...)
In my opinion IsSigned< T >...
is a dependent template parameter, therefore it can not be checked against §17.7 (8.3) in template definition time. IsSigned< T >
could be for example void
for one subset of Ts, int
for another subset or substitution failure. For the void
subset it is true, that the empty template parameter pack would be the only valid specialization, but the int
subset could have many valid specializations. It depends on the actual T
argument.
It means that the compiler must check it after the template instantiation, because T is not known before. At that point the full argument list is known, there is zero variadic arguments. The standard says the following (§17.6.3 (7)):
When N is zero, the instantiation of the expansion produces an empty list. Such an instantiation does not alter the syntactic interpretation of the enclosing construct
This is why I think it is well formed.
The code is ill-formed, no diagnostic is required.
If std::is_signed_v<T>
, then std::enable_if_t<std::is_signed_v<T>>
denotes the type void
. Otherwise, std::enable_if_t<std::is_signed_v<T>>
does not denote a valid type. Therefore, every valid specialization of myAbs
requires an empty template parameter pack.
Per [meta.rqmts]/4, the program has undefined behavior if std::enable_if
is specialized. Therefore, the aforementioned behavior cannot be changed.
In my opinion
IsSigned< T >...
is a dependent template parameter, therefore it can not be checked against §17.7 (8.3) in template definition time.IsSigned< T >
could be for examplevoid
for one subset ofT
s,int
for another subset or substitution failure. For thevoid
subset it is true, that the empty template parameter pack would be the only valid specialization, but theint
subset could have many valid specializations. It depends on the actualT
argument.
The compiler cannot check it, in the same way it cannot, say, solve an arbitrary equation for you. NDR (no diagnostic required) is made exactly for such cases — the program is ill-formed and would require a diagnostic if the compiler is actually capable of detecting that. NDR permits the compiler not to check it.
When N is zero, the instantiation of the expansion produces an empty list. Such an instantiation does not alter the syntactic interpretation of the enclosing construct.
The rule we are talking about is a semantic rule, not a syntactic rule, because syntactic rules are in [gram].
So what is the rationale for the NDR rules? In general, they address problems that are not reproducible among implementation strategies. For example, they may cause the code to misbehave in some implementation strategies, but do not cause any problems (and cannot be easily) in others.
Also, note that the standard talks in terms of program with terms like "ill-formed". Therefore, it is not always plausible to talk about the well-formed ness of an isolated code snippet. In this case, std::enable_if
is required not to be specialized, but the situation may get more complicated otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With