I'm trying to use template specialization so that I can have specialized behavior for different types. However, I'm unable to get a template specialization for a string literal type (const char[N]
) to bind to the specialized template.
I have a simple template select_type<T>
, with the following specializations:
template <class T>
struct select_type
{
static void action()
{
cout << "Generic type" << endl;
}
};
template <>
struct select_type<std::string>
{
static void action()
{
cout << "Specialization for string" << endl;
}
};
template <std::size_t N>
struct select_type<const char[N]>
{
static void action()
{
cout << "Specialization for const char array" << endl;
}
};
When I attempt to instantiate each specialization as follows:
select_type<int>::action();
select_type<std::string>::action();
select_type<decltype("abc")>::action();
... I get the following output:
Generic type
Specialization for string
Generic type
Note that the specialization for char
arrays is not invoked, even though decltype(abc)
should produce the type const char[4]
.
I thought that perhaps some type of type decay was occurring, so I added in a specialization for const char*
, but it still wasn't selected.
So, why does the expression:
select_type<decltype("abc")>::action();
fail to invoke the specialization for const char[N]
?