Constexpr 转换为 const char[]
Constexpr cast to const char[]
很多人都遇到过,我也遇到过。我在 C++ 中使用编译时字符串时遇到了问题。
我决定采用显然无法使用的方法:使用 template <char...>
类.
这是我想出来的,很普通,没什么特别的,而且也不行。
template <char... chars> class string
{
public:
static constexpr const char value[] = {chars...};
constexpr string()
{
}
constexpr operator decltype(value) & () const
{
return value;
}
};
template <char... chars> constexpr const char string <chars...> :: value[];
我的想法是制作一个 string
实例 constexpr
可构造的并公开某种 constexpr 转换,以便它提供其内容。
现在,如果我这样做
static constexpr const char x[] = "ciao";
template <const char * str> void print()
{
std :: cout << str << std :: endl;
}
print <x> ();
这有效并表示 ciao
。如果我这样做,我也会得到 ciao
std :: cout << string <'c', 'i', 'a', 'o'> {} << std :: endl;
或
print <string <'c', 'i', 'a', 'o', '[=13=]'> :: value> ();
但是当我这样做的时候
print <string <'c', 'i', 'a', 'o', '[=14=]'> {}> ();
我得到:No matching function for call to print
.
我肯定漏掉了什么。做我想做的事是不可行的吗?使一个实例以某种方式执行 constexpr 转换 return value
?如果可行,我将能够在编译时轻松地进行运算符和字符串操作,"only" 缺点是超级无聊 'i', 'n', 'i', 't', 'i', 'a', 'l', 'i', 'z', 'a', 't', 'i', 'o', 'n'
.
进一步的实验
我做了另一个完美的实验。
template <char... chars> class string
{
public:
constexpr string()
{
}
constexpr operator size_t () const
{
return sizeof...(chars);
}
};
template <size_t length> void print()
{
std :: cout << length << std :: endl;
}
print <string <'c', 'i', 'a', 'o'> {}> ();
它打印出漂亮的 4
。
模板参数匹配不考虑用户定义的转换。
但是,
print <string <'c', 'i', 'a', 'o', '[=10=]'>{} > ();
…你可以写
print <string <'c', 'i', 'a', 'o', '[=11=]'>::value> ();
或者你可以定义
template <const char* str>
void print()
{
std::cout << str << std::endl;
}
template< class Type >
void print()
{
print<Type::value>();
}
...然后只写
print <x> ();
print <string <'c', 'i', 'a', 'o', '[=13=]'>> ();
… 其中 x
是您的命名空间范围
static constexpr const char x[] = "ciao";
注意:此代码使用 MinGW g++ 5.1.0 和 -std=c++14
编译,但不使用 Visual C++ 2015 update 2。这种差异对于推送的代码很常见语言的边界。标准在不断变化,编译器也在不断发展以努力跟上标准,因此这样的代码在实践中不是很便携。
在 C++14 中,模板实参对模板非类型参数的限制是:
A template-argument for a non-type, non-template template-parameter shall be one of:
* for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or
* the name of a non-type template-parameter; or
* a constant expression (5.19) that designates the address of a complete object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as &
id-expression, where the id-expression is the name of an object or function, except that the &
may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
* a constant expression that evaluates to a null pointer value (4.10); or
* a constant expression that evaluates to a null member pointer value (4.11); or
* a pointer to member expressed as described in 5.3.1; or
* a constant expression of type std::nullptr_t
.
在您的示例中,string<'c', 'i', 'a', 'o', '[=16=]'>{}
是其中的 none。请注意,第一个项目符号仅限于整数或枚举类型,而 const char*
两者都不是(整数类型的例外是允许您的 "Further experiments" 示例编译)。 None 的其他项目符号适用。因此,符合标准的 C++14 应该 拒绝您的代码。
解决方法是直接传递底层字符数组:
print<string<'c', 'i', 'a', 'o', '[=10=]'>::value>();
或者将类型本身作为模板type参数,并在函数内打印::value
:
template <class T>
void print() {
std::cout << T::value << std::endl;
}
print<string<'c', 'i', 'a', 'o', '[=11=]'>>();
或者...
你会很高兴知道在 C++17 中,事情变得更好了。参见 N4198,带有激励示例:
template<int *p> struct A {};
int n;
A<&n> a; // ok
constexpr int *p() { return &n; }
A<p()> b; // error
该论文提出了对模板非类型参数的较少限制,这将使上述示例合式。新的措辞如下:
A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter. For a non-type template-parameter of reference type or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):
* a subobject (1.8),
* a temporary object (12.2),
* a string literal (2.14.5),
* the result of a typeid
expression (5.2.8), or
* a predefined __func__
variable (8.4.1).
就是这样。其他一切都是允许的。
由于 string<'c', 'i', 'a', 'o', '[=16=]'>{}
转换为指针,即 none 这些东西,示例变得合式。 Clang 3.8 将在 c++1z 模式下(正确)编译您的示例,但不会在 c++14 模式下(正确)编译。 GCC 还没有实现这个。给他们时间。
很多人都遇到过,我也遇到过。我在 C++ 中使用编译时字符串时遇到了问题。
我决定采用显然无法使用的方法:使用 template <char...>
类.
这是我想出来的,很普通,没什么特别的,而且也不行。
template <char... chars> class string
{
public:
static constexpr const char value[] = {chars...};
constexpr string()
{
}
constexpr operator decltype(value) & () const
{
return value;
}
};
template <char... chars> constexpr const char string <chars...> :: value[];
我的想法是制作一个 string
实例 constexpr
可构造的并公开某种 constexpr 转换,以便它提供其内容。
现在,如果我这样做
static constexpr const char x[] = "ciao";
template <const char * str> void print()
{
std :: cout << str << std :: endl;
}
print <x> ();
这有效并表示 ciao
。如果我这样做,我也会得到 ciao
std :: cout << string <'c', 'i', 'a', 'o'> {} << std :: endl;
或
print <string <'c', 'i', 'a', 'o', '[=13=]'> :: value> ();
但是当我这样做的时候
print <string <'c', 'i', 'a', 'o', '[=14=]'> {}> ();
我得到:No matching function for call to print
.
我肯定漏掉了什么。做我想做的事是不可行的吗?使一个实例以某种方式执行 constexpr 转换 return value
?如果可行,我将能够在编译时轻松地进行运算符和字符串操作,"only" 缺点是超级无聊 'i', 'n', 'i', 't', 'i', 'a', 'l', 'i', 'z', 'a', 't', 'i', 'o', 'n'
.
进一步的实验
我做了另一个完美的实验。
template <char... chars> class string
{
public:
constexpr string()
{
}
constexpr operator size_t () const
{
return sizeof...(chars);
}
};
template <size_t length> void print()
{
std :: cout << length << std :: endl;
}
print <string <'c', 'i', 'a', 'o'> {}> ();
它打印出漂亮的 4
。
模板参数匹配不考虑用户定义的转换。
但是,
print <string <'c', 'i', 'a', 'o', '[=10=]'>{} > ();
…你可以写
print <string <'c', 'i', 'a', 'o', '[=11=]'>::value> ();
或者你可以定义
template <const char* str>
void print()
{
std::cout << str << std::endl;
}
template< class Type >
void print()
{
print<Type::value>();
}
...然后只写
print <x> ();
print <string <'c', 'i', 'a', 'o', '[=13=]'>> ();
… 其中 x
是您的命名空间范围
static constexpr const char x[] = "ciao";
注意:此代码使用 MinGW g++ 5.1.0 和 -std=c++14
编译,但不使用 Visual C++ 2015 update 2。这种差异对于推送的代码很常见语言的边界。标准在不断变化,编译器也在不断发展以努力跟上标准,因此这样的代码在实践中不是很便携。
在 C++14 中,模板实参对模板非类型参数的限制是:
A template-argument for a non-type, non-template template-parameter shall be one of:
* for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or
* the name of a non-type template-parameter; or
* a constant expression (5.19) that designates the address of a complete object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as&
id-expression, where the id-expression is the name of an object or function, except that the&
may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
* a constant expression that evaluates to a null pointer value (4.10); or
* a constant expression that evaluates to a null member pointer value (4.11); or
* a pointer to member expressed as described in 5.3.1; or
* a constant expression of typestd::nullptr_t
.
在您的示例中,string<'c', 'i', 'a', 'o', '[=16=]'>{}
是其中的 none。请注意,第一个项目符号仅限于整数或枚举类型,而 const char*
两者都不是(整数类型的例外是允许您的 "Further experiments" 示例编译)。 None 的其他项目符号适用。因此,符合标准的 C++14 应该 拒绝您的代码。
解决方法是直接传递底层字符数组:
print<string<'c', 'i', 'a', 'o', '[=10=]'>::value>();
或者将类型本身作为模板type参数,并在函数内打印::value
:
template <class T>
void print() {
std::cout << T::value << std::endl;
}
print<string<'c', 'i', 'a', 'o', '[=11=]'>>();
或者...
你会很高兴知道在 C++17 中,事情变得更好了。参见 N4198,带有激励示例:
template<int *p> struct A {};
int n;
A<&n> a; // ok
constexpr int *p() { return &n; }
A<p()> b; // error
该论文提出了对模板非类型参数的较少限制,这将使上述示例合式。新的措辞如下:
A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter. For a non-type template-parameter of reference type or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):
* a subobject (1.8),
* a temporary object (12.2),
* a string literal (2.14.5),
* the result of atypeid
expression (5.2.8), or * a predefined__func__
variable (8.4.1).
就是这样。其他一切都是允许的。
由于 string<'c', 'i', 'a', 'o', '[=16=]'>{}
转换为指针,即 none 这些东西,示例变得合式。 Clang 3.8 将在 c++1z 模式下(正确)编译您的示例,但不会在 c++14 模式下(正确)编译。 GCC 还没有实现这个。给他们时间。