拥有 constexpr 静态字符串会导致链接器错误

Having a constexpr static string gives a linker error

下面的程序给我一个 link-time 错误:

#include <iostream>

struct Test { static constexpr char text[] = "Text"; };

int main()
{
    std::cout << Test::text << std::endl; // error: undefined reference to `Test::text'
}

错误信息是

/tmp/main-35f287.o: In function `main':
main.cpp:(.text+0x4): undefined reference to `Test::text'
main.cpp:(.text+0x13): undefined reference to `Test::text'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

好的。让我们尝试解决这个问题:我在 struct 正文之外添加了一个定义:

#include <iostream>

struct Test { static constexpr char text[] = "Text"; };
constexpr char Test::text[] = "Text";

int main()
{
    std::cout << Test::text << std::endl;
}

Clang 给我以下错误信息。

main.cpp:4:35: error: static data member 'text' already has an initializer
    constexpr char Test::text[] = "Text";
                                  ^
main.cpp:3:50: note: previous initialization is here
    struct Test { static constexpr char text[] = "Text"; };

哦,好吧,我想,现在我知道你想要什么了:

#include <iostream>

struct Test { static constexpr char text[]; };
constexpr char Test::text[] = "Text";

int main()
{
    std::cout << Test::text << std::endl;
}

又是一个错误:

main.cpp:3:41: error: declaration of constexpr static data member 'text' requires an initializer
    struct Test { static constexpr char text[]; };

然后狗咬了自己的尾巴。 :(

有没有办法使用在 class 中声明的 compile-time 常量字符数组?我想要 class 中的数据的原因是我需要一个类型特征 class 来帮助我做模板的事情。

应该有效:

#include <iostream>

struct Test { static constexpr char text[] = "Text"; };
constexpr char Test::text[];

int main()
{
    std::cout << Test::text << std::endl;
}

在标准 (n4140 §9.4.2/3) 中,您可以找到:

A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. —end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.

如评论所述,此版本运行良好:

struct Test { static constexpr auto text = "Text"; };

但是 text 将是 const char* 而不是 char[]