简体   繁体   English

C ++模板将const char数组转换为int

[英]C++ Template const char array to int

So, I'm wishing to be able to have a static const compile time struct that holds some value based on a string by using templates. 因此,我希望能够拥有一个静态const编译时结构,该结构通过使用模板来保存基于字符串的某些值。 I only desire up to four characters. 我只希望最多四个字符。 I know that the type of 'abcd' is int , and so is 'ab','abc', and although 'a' is of type char , it works out for a template<int v> struct 我知道'abcd'的类型为int ,'ab','abc'的类型也是如此,尽管'a'的类型为char ,但它适用于template<int v> struct

What I wish to do is take sizes of 2,3,4,5 of some const char, "abcd" and have the same functionality as if they used 'abcd'. 我希望做的是,使用一些const char,“ abcd”的大小为2,3,4,5,并具有与使用“ abcd”相同的功能。 Note that I do not mean 1,2,3, or 4 because I expect the null terminator. 请注意,我并不是指1,2,3或4,因为我希望使用空终止符。

cout << typeid("abcd").name() << endl; tells me that the type for this hard coded string is char const [5] , which includes the null terminator on the end. 告诉我,此硬编码字符串的类型为char const [5] ,其末尾包含空终止符。

I understand that I will need to twiddle the values as characters, so they are represented as an integer. 我知道我需要将这些值当作字符来缠绕,因此它们以整数表示。

I cannot use constexpr since VS10 does not support it (VS11 doesn't either..) 我无法使用constexpr因为VS10不支持它(VS11也不支持。)

So, for example with somewhere this template defined, and later the last line 因此,例如,在定义此模板的某个位置,然后在最后一行

template <int v> struct something {
    static const int value = v;
};

//Eventually in some method
cout << typeid(something<'abcd'>::value).name() << endl;

works just fine. 效果很好。

I've tried 我试过了

template<char v[5]> struct something2 {
    static const int value = v[0];
}

template<char const v[5]> struct something2 {
    static const int value = v[0];
}

template<const char v[5]> struct something2 {
    static const int value = v[0];
}

All of them build individually, though when I throw in my test, 它们都是单独构建的,尽管当我进行测试时,

cout << typeid(something2<"abcd">::value).name() << endl;

I get 我懂了

'something2' : invalid expression as a template argument for 'v'
'something2' : use of class template requires template argument list

Is this not feasible or am I misunderstanding something? 这是不可行的,还是我误会了什么?

14.1 lists the acceptable types of non-type template arguments: 14.1列出了非类型模板参数的可接受类型:

— integral or enumeration type, —整数或枚举类型,
— pointer to object or pointer to function, —指向对象或函数的指针,
— lvalue reference to object or lvalue reference to function, —对对象的左值引用或对函数的左值引用,
— pointer to member, —指向成员的指针,

Arrays don't fit under any of these categories. 数组不属于任何这些类别。

14.3.2/1 lists categories of what's permitted as template arguments and 14.3.2/2 goes on to say: 14.3.2 / 1列出了允许用作模板参数的类别,而14.3.2 / 2继续说:

Note: A string literal (2.14.5) does not satisfy the requirements of any of these categories and thus is not an acceptable template-argument. 注意:字符串文字(2.14.5)不满足任何这些类别的要求,因此不是可接受的模板参数。

Therefore you cannot do what you're trying to do. 因此,您无法做您想做的事情。

you cannot, from the standard. 您不能,从标准来看。

14.3.2.1: 14.3.2.1:

A template-argument for a non-type, non-template template-parameter shall be one of: 非类型,非模板模板参数的模板参数应为以下之一:

  • an integral constant-expression of integral or enumeration type; 整数或枚举类型的整数常量表达式; or 要么
  • the name of a non-type template-parameter; 非类型模板参数的名称; or 要么
  • the address of an object or function with external linkage, including function templates >and function template-ids but excluding non-static class members, expressed as & id->expression where the & is optional if the name refers to a function or array, or if the >corresponding template-parameter is a reference; 具有外部链接的对象或函数的地址,包括函数模板>和函数模板ID,但不包括非静态类成员,表示为&id-> expression,如果名称是指函数或数组,则&是可选的,或者>相应的模板参数是参考; or 要么
  • a pointer to member expressed as described in 5.3.1 . 指向5.3.1中描述的成员的指针。

There is a way to get close to what you are wanting. 有一种方法可以接近您想要的东西。 Possibly this will work for you, although it adds an extra level of code to be maintained. 尽管它增加了要维护的额外代码级别,但可能对您有用。

It requires defining const char arrays with external linkage, then using the names of those arrays to instantiate classes from the templates. 它需要使用外部链接定义const char数组,然后使用这些数组的名称从模板实例化类。 Of course in real use this code would be separated into various .h and .cpp files. 当然,在实际使用中,此代码将分成多个.h和.cpp文件。

extern const char a[] = "a";
extern const char b[] = "b";
extern const char ab[] = "ab";
extern const char abc[] = "abc";

template <const char * const  T> class Test
{
public:
    Test() {str = typename T;};
private:
    const char * str;
};


SomeFunction()
{
    Test<a> A;
    Test<b> B;
    Test<ab> AB;
    Test<abc> ABC;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM