小编y0p*_*rst的帖子

静态constexpr成员的未定义引用错误

考虑以下代码:

#include <vector>

struct A {
  static constexpr int kDefaultValue = -1;
  std::vector<int> v;
  A(int n): v(n, A::kDefaultValue) {}
};

int main() {
  A(10);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它无法链接(llvm clang,gcc 4.9,两者都在OS X上):

Undefined symbols for architecture x86_64:
  "A::kDefaultValue", referenced from:
      A::(int) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

问题是它有什么问题?它可以通过static_cast-ing A::kDefaultValue来修复int.或者搬出kDefaultValueA.两个案件似乎都很难看.这是另一种使它链接的方式吗?

c++ definition constexpr c++17

17
推荐指数
2
解决办法
6358
查看次数

标签 统计

c++ ×1

c++17 ×1

constexpr ×1

definition ×1