(this question is not related to C++11/C++14: the examples are compiled using C++03)
enable_bool<T>
has a member ::type
only when T
is bool
template <class T>
struct enable_bool
{};
template <>
struct enable_bool< bool >
{ typedef bool type; };
In the next snippet, the partial specialization is correct (see gcc.godbolt.org)
template <class T, class U, class Enable = T>
struct Foo
{
static int bar() { return 0; }
};
template <class T, class U>
struct Foo< T, U, typename enable_bool<T>::type >
{
static int bar() { return 1; }
};
int main()
{
return Foo <int, bool>::bar();
}
As enable_bool<T>::type
already corresponds to T
(when T
is bool
)
we are tempted to factorize parameters T
and Enable
.
But compiler complains (see gcc.godbolt.org)
template <class T, class U>
struct Foo
{
static int bar() { return 0; }
};
template <class T, class U> //ERROR non-deducible template parameter 'T'
struct Foo< typename enable_bool<T>::type, U >
{
static int bar() { return 1; }
};
Why compiler cannot deduce template parameter T
in this above partial specialization?
std::enable_if
actually works. No, it won't "evolve" in C++17. – Lightness Races in Orbitstd::enable_if<>::type
accesses a non-existing type, so that declaration is ill-formed". This is maybe the answer of my question. I wonder about the C++ Standard/Compiler reason to do not allow one single parameter for SFINAE (usingstd::enable_if
or not). Please reopen my question. Cheers – oHostd::enable_if
works. Therefore, to answer the question, you should read howstd::enable_if
works; certainly there is little to gain in our repeating all of that here. I did actually read the answers there (albeit briefly), and confirmed that they make reference to this particular requirement, within the broader scope and context of the feature in question. – Lightness Races in Orbitstd::enable_if
documentation. But it is not obvious where the answer is. (Please give me the answer). I think it will not help SO users to be redirected to the other question (because the answer of my question is not blinking there). I have fully rewritten my question since first post. My question is not related to C++11 neither tostd::enable_if
. While rewriting/testing the code I got a light. I think the answer is because compiler needs to know where is the parameterint
. If you reopen the question I can provide an answer. – oHo