为模板特化选择字符串文字类型

Selecting string literal type for template specialization

我正在尝试使用模板专业化,以便我可以针对不同的类型拥有专门的行为。但是,我无法获得字符串文字类型 (const char[N]) 的模板特化以绑定到专用模板。

我有一个简单的模板 select_type<T>,具有以下专业化:

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;
    }
};

当我尝试按如下方式实例化每个特化时:

select_type<int>::action();
select_type<std::string>::action();
select_type<decltype("abc")>::action();

...我得到以下输出:

Generic type
Specialization for string
Generic type

请注意,char 数组的特化不会被调用,即使 decltype(abc) 应该生成类型 const char[4]

我认为可能发生了某种类型的衰减,所以我为 const char* 添加了一个专业化,但它仍然没有被选中。

那么,为什么表达式:

select_type<decltype("abc")>::action();

无法调用 const char[N] 的专业化?

您看到此行为是因为 decltype 推导类型的方式。字符串文字是左值。来自 [expr.prim.general]/p1:

A string literal is an lvalue; all other literals are prvalues.

decltype() returns 左值的左值引用类型。 [dcl.type.simple]/p4

For an expression e, the type denoted by decltype(e) is defined as follows:

(4.1) — if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;

(4.2) — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;

(4.3) — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;

(4.4) — otherwise, decltype(e) is the type of e.

所以你的专业需要如下:

template <std::size_t N>
struct select_type<const char (&)[N]>