gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:52 28 4
gpt4 key购买 nike

下面的程序给我一个链接时错误:

#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[]; };

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

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

最佳答案

应该工作:

#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.

关于c++ - 拥有 constexpr 静态字符串会导致链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com