2016-07-14 125 views
-1

我有以下類C++ 11別名和模板的模板

struct Abis 
{ 
    void foo() { // does something } 
}; 

struct A 
{ 
    typedef Abis bis_type; 
    bis_type create() { // instanciates Abis object }; 

}; 

template<class Tbis> // typically Tbis would be Abis 
struct Bbis 
{ 
    // something 
}; 

template<class T> // typically T would be A 
struct B 
{ 
    typedef Bbis<typename T::bis_type> bis_type; 
    bis_type create() { // instanciates Bbis object }; 
}; 

現在我想添加其他層:

template<class Tbis, template<class> class Ubis> 
struct Cbis : public Ubis<Tbis> 
{ 
    void additional_method() { // does something calling Tbis and Ubis methods} 
}; 

和具有C類實例化對象CBIS所以我可以寫這樣的事情:

C<A,B> c(); 
Cbis<Abis,Bbis> cbis = c.create(); 
cbis.additional_method(); 

這需要能夠參考的typedef bis_types所以我想這

template<class T, template<class> class U> 
struct C 
{ 
    template<class S> 
    using Ubis_type = U<S>::bis_type // PROBLEM! 
    typedef Cbis<typename T::bis_type,Ubis_type> bis_type; 

    bis_type create(); 
} 

這似乎並沒有工作,而且也不

template<class T, template<class> class U> 
struct C 
{ 
    template<class S> 
    using Ubis_type = typename U<S>::bis_type // PROBLEM! 
    typedef Cbis<typename T::bis_type,Ubis_type> bis_type; 

    bis_type create(); 
} 

作爲一個臨時的解決辦法,我可以通過B型作爲模板參數CBIS但是這並沒有真正尊重設計,我想了解問題是什麼。

我的語法正確嗎? (我用的XCode 7.3)

感謝

+0

你已經關閉了錯誤的開始,你得到了甚至在走出大門:'typedef的巴士轉車站bis_type;' - 「Bbis」是不是一種類型。這是一個模板。巨大差距。當然「U 」是「一個問題!」沒有在任何地方定義的模板U.所有這些看起來都像幻想代碼,而不是真正的代碼。 –

+0

*「這似乎不工作,也沒有」*,包括錯誤消息 –

+0

模板 //它沒有意義,因爲您根本不使用T類...... – nosbor

回答

0

的主要問題是與你的typedef Bbis bis_type線 - Bbis是一個類模板,而不是一類。我認爲最有可能的是你的意思是typedef Bbis<T> bis_type(鑑於你在這種情況下有一個類型T)。在這之後,你的代碼主要工作(我不得不糾正了幾個錯別字得到它來編譯),這裏是最後的版本:

UPDATE:的struct C改變定義,以適應與C<A, B>要求的使用情況)

struct Abis 
{ 
    void foo() { // does something 
    } 
}; 

struct A 
{ 
    typedef Abis bis_type; 
    bis_type create() { // instanciates Abis object 
    return Abis(); 
    } 

}; 

template<class Tbis> // typically Tbis would be Abis 
struct Bbis 
{ 
    // something 
}; 

template<class T> // typically T would be A 
struct B 
{ 
    typedef Bbis<T> bis_type; 
    bis_type create() { // instanciates Bbis object 
    }; 
}; 

template<class Tbis, template<class> class Ubis> 
struct Cbis : public Ubis<Tbis> 
{ 
    void additional_method() { // does something calling Tbis and Ubis methods 
    } 
}; 

template<class T, template<class> class U> 
struct C 
{ 
    template<class S> 
    using Ubis_type = typename U<S>::bis_type; // PROBLEM! 

    typedef Cbis<typename U<T>::bis_type,Ubis_type> bis_type; 

    bis_type create() 
    { 
     return bis_type();  
    } 
}; 

然後,你可以用你的最終struct C這樣的:

int main(int argc, char**args) 
{ 
    C<A, B> c; 
    auto d = c.create(); 

    d.additional_method(); 

    return 0; 
} 
+0

感謝您的答案。我編輯我的帖子,以糾正B :: bis_type(這是在我的實際代碼中正確定義,所以這不是問題)的定義。我還舉了一個我希望達到的最終結果的例子。 – AFK

+0

查看我上面的編輯。您現在可以根據需要使用'C '。注意:您(手動)扣除'C :: create()'的返回類型是錯誤的。但是,你可以使用'auto'而不用擔心它。最後一個對象'd'完全可用,並且可以在其上調用'additional_method',正如您在我的回答中看到的那樣 – Smeeheey

+0

好吧,您只是說我的語法正確,只要我對依賴類型使用typename? – AFK