C++11 别名和模板模板
C++11 alias and template template
>我有以下类
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)
谢谢
主要问题在于您的typedef Bbis bis_type
行 - Bbis
是类模板而不是类。我认为您的意思很可能是typedef Bbis<T> bis_type
(鉴于您在该上下文中具有类型T
)。之后,您的代码基本上可以正常工作(我必须更正一些拼写错误才能对其进行编译),这是最终版本:
(更新:更改了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;
}
相关文章:
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- C++ 11 个模板,参数包的别名
- C++11 模板解析错误,使用模板别名键入推导
- c++11 严格别名规则是否允许通过 char *、char(&)[N],甚至 std::array<char、N> 和 -fstrict-aliasing -Wstrict-aliasi
- Doxygen 对 C++11 模板别名的支持("using"语法)?
- C++11中一个专用模板的名称别名
- C++11`using`关键字:专门化模板参数的模板别名
- 在 C++11 中使用严格的混叠时,它是否定义为 _write_ 到 char*,然后从别名的非 char* 读取_
- C++11 模板函数别名与包装器
- C++11 模板别名作为模板模板参数会导致不同的类型
- C++11 模板类型别名以减少痛苦
- C++11:如何对函数进行别名
- 为什么C++11类型特征不是别名模板
- c++ 11在CUDA中的别名模板
- c++ 11中的const类型转换为const别名
- 当基类具有两个或多个模板参数时,C++11 别名基模板类变量在模板派生类中不起作用
- 如何在c++ 11中使用右值复制或别名对象
- 如何在C++11中访问可能不存在的类型别名
- C++11中的别名结构
- C++11 别名和模板模板