我正在尝试在constexpr
中使用make_tuple()
。它在全球范围内运作。但它会为static constexpr
类成员生成链接错误。如何在静态constexpr类成员中使用make_tuple()?
#include <iostream>
#include <tuple>
using namespace std;
class A
{
public:
static constexpr auto z = make_tuple(5, 3.0);
};
constexpr auto tp = make_tuple(6, 3.2);
int main()
{
cout << get<0>(tp) << " " << get<1>(tp) << endl; // OK
cout << get<0>(A::z) << " " << get<1>(A::z) << endl; // error: (.text+0x5a): undefined reference to `A::z'
// (.text+0x67): undefined reference to `A::z'
}
我已经检查heremake_tuple
它本身也不是一个c++11
constexpr
。我猜这在这种情况下不是问题。如果是的话,它会产生一个编译错误,而不是链接错误。
我试图定义constexpr
类的外部波纹一样通过this answer
class A
{
public:
static constexpr tuple<int, double> z;
};
constexpr tuple<int, double> A::z = make_tuple(5, 3.0);
的建议不过,它会产生几个编译错误。这使得根据回答constexpr initializing static member using static function
在static constexpr
类成员中使用make_tuple
的正确方法是什么?
编译器: g++ 4.8.4
和clang 3.4
与-std=c++11
[很适合我](http://coliru.stacked-crooked.com/a/bc0d0fef61b17c97)。 – user0042
悲伤地不与非内在的对象。使用静态constexpr函数来传递对象。 –
这个编译器的g ++版本是什么? – army007