单个刻度线中的C ++模板字符作为参数

时间:2022-02-06 07:19:17

I make it short. The code:

我做得很短。代码:

typedef int NAME;

template <NAME, typename T>
class MyTemplate
{
public:

    MyTemplate(T value)
    {
        value_type = value;
    }

    T value_type;

    operator T& const ()
    {
        return value_type;
    }
};

And in in main:

在主要:

MyTemplate<'val', int> number1(1);

I tumbled across this kind of code a while ago and I just rebuilt it by memorizing. What is confusing to me is the val as the first template argument. Also, if I pass more than 4 characters the compiler will complain. So what could be the purpose of the NAME in the template and why can I pass a string in single ticks? I'm also not sure if the code I saw a while ago had the typedef in it but without it it won't compile.

我不久前翻过这种代码,我只是通过记忆来重建它。令我困惑的是val作为第一个模板参数。此外,如果我传递超过4个字符,编译器会抱怨。那么模板中NAME的目的是什么?为什么我可以在单个刻度中传递一个字符串?我也不确定我刚才看到的代码是否有typedef,但没有它就无法编译。

1 个解决方案

#1


3  

'val' is a multi-character literal. Its type is int (rather than char, as ordinary character literals use in C++). its numeric value is implementation-defined. It is distinct from a string literal.

'val'是一个多字符文字。它的类型是int(而不是char,就像普通字符文字在C ++中使用的那样)。它的数值是实现定义的。它与字符串文字不同。

#1


3  

'val' is a multi-character literal. Its type is int (rather than char, as ordinary character literals use in C++). its numeric value is implementation-defined. It is distinct from a string literal.

'val'是一个多字符文字。它的类型是int(而不是char,就像普通字符文字在C ++中使用的那样)。它的数值是实现定义的。它与字符串文字不同。