2017-07-22 7 views
1

指定された列挙テンプレートパラメータ(store_type)に基づいてクラステンプレートを選択しようとしています。今、私はこれを使用するクラスをインスタンス化しますが、このクラスのbasic_storeをインスタンス化しようとするようです。enumテンプレートパラメータによるコンパイル時間クラステンプレート選択

enum store_type 
{ 
    none, 
    basic, 
    lockless, 
}; 

template<class T, store_type S = none, typename = void> 
struct get_store_type 
{ 
}; 

template<class T> 
struct get_store_type<T, basic, 
typename std::enable_if<!std::is_abstract<T>::value>::type> 
{ 
    using store_type = typename basic_store<T>; 
}; 

template<class T> 
struct get_store_type<T, lockless> 
{ 
    using store_type = typename lockless_store<T>; 
}; 

template<typename T, store_type S> 
class client 
{ 
public: 
    using my_store_type = typename get_store_type<T, S>::store_type; 
} 

//Tries to instantiate a basic store... which is not allowed. 
client<SomeAbstractType, lockless> something; 

答えて

3

あなたは専門で第三テンプレート引数を忘れてしまいました。

template<class T> struct get_store_type<T, lockless, void > 
                ^^^^ 

次のコードの出力は、、及びある:

#include <iostream> 

enum store_type { none, basic, lockless }; 

template<class T, store_type S = none, typename = void> 
struct get_store_type 
{ int a = 1; }; 

template<class T> 
struct get_store_type<T, basic, typename std::enable_if<!std::is_abstract<T>::value>::type> 
{ int b = 2; }; 

template<class T> 
struct get_store_type<T, lockless, void > 
{ int c = 3; }; 

struct Any{}; 

int main(void) 
{ 
    get_store_type<int> storeA; 
    get_store_type<Any, basic> storeB; 
    get_store_type<int, lockless> storeC; 

    std::cout << storeA.a << std::endl; 
    std::cout << storeB.b << std::endl; 
    std::cout << storeC.c << std::endl;  

    return 0; 
} 
+0

勿論!ありがとう、年齢のためにこれを見つめていた:P。 –

+0

@AndreasLoanjoe大歓迎です。 – Rabbid76

+1

私はそれをどのように受け入れるのですか?私はそれをupvoteする必要がありますか?私がしたから。編集:nvmはそれが受け入れられたそれを得た! –

関連する問題