具有 constexpr 静态字符串会给出链接器错误
Having a constexpr static string gives a linker error
以下程序给了我一个链接时间错误:
#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[]
.
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误