c++ - Constexpr 转换为 const char[]

标签 c++ templates char constexpr

很多人都遇到过,我也遇到过。我在 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', '\0'> :: value> ();

但是当我这样做的时候

print <string <'c', 'i', 'a', 'o', '\0'> {}> ();

我得到:No matching function for call to print .

我肯定遗漏了什么。做我想做的事是不可行的吗?让实例执行 constexpr 转换以某种方式返回 value ?如果可行,我将能够在编译时轻松地进行运算符和字符串操作,“唯一”的缺点是 super 无聊 '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 .

最佳答案

在 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', '\0'>{}这些都不是。请注意,第一个项目符号仅限于整数或枚举类型,并且 const char*两者都不是(整数类型的异常(exception)是允许您的“进一步实验”示例编译)。其他项目符号均不适用。因此,符合标准的 C++14 应该拒绝您的代码。

解决方法是直接传递底层字符数组:

print<string<'c', 'i', 'a', 'o', '\0'>::value>();

或者将类型本身作为模板类型参数,并打印::value在函数内:

template <class T>
void print() {
    std::cout << T::value << std::endl;
}

print<string<'c', 'i', 'a', 'o', '\0'>>();

或者...


你会很高兴知道在 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', '\0'>{}转换为一个不是那些东西的指针,这个例子就变得合式了。 Clang 3.8 将在 c++1z 模式下(正确)编译您的示例,但不会在 c++14 模式下(正确)编译。 GCC 还没有实现这个。给他们时间。

关于c++ - Constexpr 转换为 const char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173469/

相关文章:

c - char 的按位运算给出 32 位结果

c++ - 派生类是否间接继承基类的赋值运算符?

c++ - 递归 vector 模板

c++ - 类对象(char)在函数后发生变化

c++ - 如何使用模板化构造函数定义推导指南?

postgresql - Go:模板中来自 PostgreSQL 的变量不是输出值(Echo 框架)

c++ - 使用 strncpy 的 ofstream 中的垃圾值

c++ - 如何在 C++ 中的单独 posix 线程中运行 OpenCV Videocapture 以实现跨平台?

c# - 将结构列表从 C# 转换为 C++

c++ - 有没有简单的方法来公开私有(private)父类 C++ 的方法