未定义的静态constexpr引用

时间:2015-10-24 20:12:04

标签: c++ c++11

在此代码段中:

template <size_t N>
struct Foo {
   static constexpr std::array<char, N> arr{{0}};
   static const char *data() { return &arr[0]; }
};

template<>
constexpr std::array<char, 5> Foo<5>::arr;

int main()
{
   std::cout << Foo<5>::data() << std::endl;
}

使用gcc 5.2我得到了对Foo<5ul>::arr的未定义引用,而clang 3.7给出了编译时错误:

  

constexpr静态数据成员'arr'的声明需要初始值设定项

有什么问题,应该如何在类声明之外定义static constexpr

1 个答案:

答案 0 :(得分:7)

外线定义与其他静态(非整数)成员相同,减去初始化:

template<size_t N>
constexpr std::array<char, N> Foo<N>::arr;

与其他静态成员一样,它位于标题中 - 就像类模板本身一样。