我正在嘗試在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