c++ 类的静态内联成员初始化正常使用g++编译,但出现clang错误

cnh2zyt3  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(158)

有一个非常简单的例子c++ project. foo.h:

class foo{  
    inline static const int array[3] = { 1, 2, 3 };  
};

foo.cc:

#include "foo.h"

foo a_foo;

main.cc:

#include "foo.h"
int main()
{
    foo b_foo;
}

使用g++通过以下命令编译时:

g++ -c *.cc
g++ *.o -o a.out

它只报告警告:

In file included from foo.cc:2:0:
foo.h:2:25: warning: inline variables are only available with -std=c++1z or -std=gnu++1z
 inline static const int array[3] = { 1, 2, 3 };
                         ^~~~~
In file included from main.cc:3:0:
foo.h:2:25: warning: inline variables are only available with -std=c++1z or -std=gnu++1z
 inline static const int array[3] = { 1, 2, 3 };
                         ^~~~~

但是当通过以下命令使用clang编译时:

clang -c *.cc
clang *.o -o a.out

甚至使用以下命令:

clang -std=c++1z -c *.cc
clang -std=c++1z *.o -o a.out

它报告错误:

In file included from foo.cc:2:
./foo.h:2:1: error: 'inline' can only appear on functions
inline static const int array[3] = { 1, 2, 3 };
^
./foo.h:2:25: error: in-class initializer for static data member of type 'const int [3]' requires 'constexpr' specifier
inline static const int array[3] = { 1, 2, 3 };
                        ^          ~~~~~~~~~~~
constexpr
2 errors generated.
In file included from main.cc:3:
./foo.h:2:1: error: 'inline' can only appear on functions
inline static const int array[3] = { 1, 2, 3 };
^
./foo.h:2:25: error: in-class initializer for static data member of type 'const int [3]' requires 'constexpr' specifier
inline static const int array[3] = { 1, 2, 3 };
                        ^          ~~~~~~~~~~~
constexpr
2 errors generated.

尝试了几次修改,但都失败了。如何纠正代码,使它也编译确定与clang?

fkaflof6

fkaflof61#

好的。我把clang升级到了15.0版本似乎解决了这个问题。谢谢你的帮助!

相关问题