有一个constexpr静态字符串给出链接器错误 [英] Having a constexpr static string gives a linker error

查看:613
本文介绍了有一个constexpr静态字符串给出链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #include< iostream& 

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

int main()
{
std :: cout< Test :: text<< std :: endl; //错误:未定义引用`Test :: text'
}

错误消息

  /tmp/main-35f287.o:在main函数中:
main.cpp :(。文本+ 0x4):未定义的引用`Test :: text'
main.cpp :(。text + 0x13):未定义引用`Test :: text'
clang:error:退出代码1(使用-v查看调用)

让我们尝试解决:我在 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'已经有一个初始化程序
constexpr char Test :: text [] =Text;
^
main.cpp:3:50:注意:以前的初始化在这里
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:声明constexpr静态数据成员'text'需要一个初始化器
struct Test {static constexpr char text []; };

那里的狗咬了自己的尾巴。 (



有没有办法使用在类中声明的编译时常数字符数组?原因,我想在类中的数据,需要一个类型traits类,帮助我做模板的东西。

解决方案

应该工作:

  #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说明符声明文字类型的静态数据成员
;如果是这样,它的声明
指定一个括号或初始化器,其中作为赋值表达式的每个初始化子句
是一个常量表达式[注意:在
这两种情况下,成员可能出现在常量表达式中。 -end
note] 如果在程序中为
odr-used(3.2),那么成员仍然在命名空间范围内定义,并且命名空间范围定义应为
不包含初始值



The following program gives me a link-time error:

#include <iostream>

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

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

The error message is

/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)

Ok. Let's try to fix that: I add a definition outside the struct body:

#include <iostream>

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

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

Clang gives me the following error message.

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"; };

Oh, well, I thought, now I know what you want:

#include <iostream>

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

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

And again an error:

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

And there the dog bites its own tail. :(

Is there a way to use compile-time constant character arrays that are declared inside a class? The reason, I want the data inside a class, is that I need a type traits class that helps me do template stuff.

解决方案

Should work:

#include <iostream>

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

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

In standard (n4140 §9.4.2/3) you can find:

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.

这篇关于有一个constexpr静态字符串给出链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆