2016-10-25 8 views
0

は、私は、構造体どのように私はテンプレート引数としてvariadic std :: tupleを取ることができますか?

template <typename Args> 
struct MyStruct 
{ 
}; 

を持っている。しかし、私は唯一例えば、std::tupleインスタンス化して、このクラスをインスタンス化することができるようにしたいと仮定します

Mystruct<std::tuple<>> a; // OK 
Mystruct<std::tuple<int, int, double>> a; // OK 
Mystruct<double> a; // FAIL 

どうすればいいですか?

答えて

4

これはかなり簡単です。宣言が、一般的なテンプレートを定義していない。その後、専門定義

template<typename T> struct Mystruct; 

:より良いエラーメッセージを持っている、krzaqの答えに加えて

template<typename... Ts> 
struct Mystruct<std::tuple<Ts...>> 
{ 
// stuff 
}; 
+0

ありがとうございました! – arman

1

を、あなたはstatic_assert

// The traits: 
template <typename T> 
struct is_a_tuple : std::false_type {}; 

template <typename ... Ts> 
struct is_a_tuple<std::tuple<Ts...>> : std::true_type {}; 

// Your structure (for error message) 
template <typename T> 
struct MyStruct 
{ 
    static_assert(is_a_tuple<T>::value, "T should be a std::tuple"); 
}; 

// Your structure (for std::tuple) 
template <typename ... Ts> 
struct MyStruct<std::tuple<Ts...>> 
{ 
    // Your implementation 
}; 
を使用する場合があります
関連する問題