具有 constexpr 静态字符串会给出链接器错误

Having a constexpr static string gives a linker error

本文关键字:链接 错误 constexpr 静态 字符串 具有      更新时间:2023-10-16

以下程序给了我一个链接时间错误:

#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) 中,您可以找到:

可以在类中声明文本类型的静态数据成员 使用constexpr说明符进行定义;如果是这样,其声明应 指定一个大括号或等于初始值设定项,其中每个初始值设定项子句 即赋值表达式是一个常量表达式。[ 注:在 在这两种情况下,成员都可能出现在常量表达式中。—完 注意 ] 如果成员是 ODR 在程序中使用的 (3.2) 和命名空间范围定义应 不包含初始值设定项

正如评论中所说,这个版本工作正常:

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

但是text将是一个const char*而不是一个char[].