未定义对静态constexpr的引用 [英] Undefined reference to static constexpr
本文介绍了未定义对静态constexpr的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此代码段中:
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给出了编译时错误:
with gcc 5.2 I got undefined reference to Foo<5ul>::arr
, while clang 3.7 gives a compile time error:
constexpr静态数据成员的声明'arr'需要初始化程序
declaration of constexpr static data member 'arr' requires an initializer
出什么问题了,静态constexpr
是否在类声明之外定义?
What is wrong, and how should static constexpr
be defined outside class declaration?
推荐答案
离线定义与其他静态定义(非整数)相同成员,减去初始化:
The out-of-line definiton is the same as for other static (non integral) members, minus the initialization:
template<size_t N>
constexpr std::array<char, N> Foo<N>::arr;
与其他静态成员一样,它位于标头中-类似于类模板本身。
Like other static members, this goes in a header - like the class template itself.
这篇关于未定义对静态constexpr的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文