相关疑难解决方法(0)

如果函数在类范围内声明,constexpr不工作

我使用的是g ++ 4.8.0,它不包含早期的constexprbug.因此下面的代码工作正常:

constexpr int size() { return 5; }
int array[size()];

int main () {}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将变量括在一个classas中static,那么它会给出编译器错误:

struct X {
  constexpr static int size() { return 5; }
  static const int array[size()]; 
};

int main () {}
Run Code Online (Sandbox Code Playgroud)

这是错误:

错误:数组'array'的大小不是整数常量表达式

禁止以constexpr这种方式使用还是使用另一个g ++ bug?

c++ compiler-errors g++ constexpr c++11

16
推荐指数
2
解决办法
4830
查看次数

内联静态成员变量

struct sa
{
  struct sb { int a = 123;};
  inline static sb b;
};
Run Code Online (Sandbox Code Playgroud)

上面的代码生成错误:

main.cpp:25:20: error: default member initializer for ‘sa::sb::a’ required before the end of its enclosing class
   inline static sb b;
                    ^
main.cpp:24:21: note: defined here
   struct sb { int a = 123;};
                     ^~~~~~
Run Code Online (Sandbox Code Playgroud)

删除inline关键字或默认成员初始化程序有效.但仅从输出,我不明白为什么这种用法是错误的.

c++ static inline c++17

8
推荐指数
1
解决办法
648
查看次数

标签 统计

c++ ×2

c++11 ×1

c++17 ×1

compiler-errors ×1

constexpr ×1

g++ ×1

inline ×1

static ×1