C++ 11别名和模板的模板

问题描述:

我有以下类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

的主要问题是与你的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

好吧,您只是说我的语法正确,只要我对依赖类型使用typename? – AFK