Is the following legal?
template<typename T>
std::enable_if_t<false, T> foo() {}
Usually, we have [temp.res]
The program is ill-formed, no diagnostic required, if:
- no valid specialization can be generated for a template [...] and the template is not instantiated [...]
Clearly, no valid specialization can be generated for foo
... unless you consider that std::enable_if
could possibly be specialized such that type
is present as void
regardless of the first bool
argument.
struct S {};
template<bool B>
struct std::enable_if<B, S> : std::type_identity<void> {};
Except... you can't specialize std::enable_if
by [meta.rqmts]
Unless otherwise specified, the behavior of a program that adds specializations for any of the templates specified in [meta] is undefined.
To summarize, if you wrote a custom enable_if
that mimics std::enable_if
, the first snippet is legal. The question is whether the prohibition on specializing std::enable_if
makes it illegal?
This post is inspired by this answer. The current form is adapted from an implementation of an independent library.