选择模板专用化的字符串文字类型 [英] Selecting string literal type for template specialization

查看:81
本文介绍了选择模板专用化的字符串文字类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  template< class T> 
struct select_type
{
static void action()
{
cout< 通用类型<< endl
}
};

模板<>
struct select_type< std :: string>
{
static void action()
{
cout< 专业化字符串<< 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();

...我得到以下输出:

 通用类型
字符串专用化
通用类型


b $ b

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



我认为可能是某种类型的衰减发生,我添加了一个专门为 const char * ,但它仍然没有选择。



表达式:

  select_type< decltype(abc)> :: action 

未能调用 const char [N]

解决方案

您会看到这种行为,因为 decltype 推导类型。字符串文字是左值。来自[expr.prim.general] / p1:


字串文字是一个左值;所有其他文字都是prvalues。


decltype()返回一个左值引用类型为左值。 [dcl.type.simple] / p4


对于表达式 e decltype(e)表示的类型定义如下:



(4.1) - 如果 e 是一个未加括号的id表达式或一个未加括号的类成员访问(5.2.5), decltype(e)命名为 e 。如果没有这样的实体,或者 e 命名一组重载函数,
程序是不成形的;


$否则,如果 e 是xvalue, decltype(e) code> T&&
,其中 T e ;



(4.3) - 否则,如果 e 是一个左值, decltype(e) T& ,其中 T e ;



(4.4) - 否则 decltype(e) e 的类型。


如下:

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


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]?

解决方案

You are seeing this behavior because of how decltype deduces the type. String literals are lvalues. From [expr.prim.general]/p1:

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

decltype() returns an lvalue-reference type for lvalues. [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.

So your specialization needs to be as follows:

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

这篇关于选择模板专用化的字符串文字类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆