C++ 如何将模板转换为C+之前的模板+;11
由我编写的模板触发,该模板允许将模板转换为具有不同参数的模板:C++ 如何将模板转换为C+之前的模板+;11,c++,templates,c++03,template-templates,C++,Templates,C++03,Template Templates,由我编写的模板触发,该模板允许将模板转换为具有不同参数的模板: template< template <int A,char B,bool> typename T> struct Add_true { template <int A,char B> using type = T<A,B,true>; }; 但是现在Add\u-true\u-to\u-Foo::type不是一个模板,如果没有C++11,我一点也不知道如何编写一个Add\u-
template< template <int A,char B,bool> typename T>
struct Add_true {
template <int A,char B> using type = T<A,B,true>;
};
但是现在Add\u-true\u-to\u-Foo::type
不是一个模板,如果没有C++11,我一点也不知道如何编写一个Add\u-true
,它不仅适用于Foo
,而且也适用于
template <int A,char B, bool C> struct Bar{};
模板结构条{};
也许我错过了一些明显的东西。我的问题是
是否可以在C++11之前编写与上述
Add_true
等价的代码?没有严格的C++11模板等价物,
但您可以添加额外的模板类来模拟行为,并使用不同的语法:
template <template <int, char, bool> class C>
struct Add_true {
template <int A, char B>
struct apply
{
typedef T<A, B, true> type;
}
};
模板
结构添加_true{
样板
结构应用
{
T型;
}
};
使用方法:
template <int A,char B, bool C> struct Foo{};
typedef Add_true<Foo>::apply<42, '*'>::type my_type; // Foo<42, '*', true>
模板结构Foo{};
typedef Add_true::apply::type my_type;//福
没有严格等效的C++11模板,
但您可以添加额外的模板类来模拟行为,并使用不同的语法:
template <template <int, char, bool> class C>
struct Add_true {
template <int A, char B>
struct apply
{
typedef T<A, B, true> type;
}
};
模板
结构添加_true{
样板
结构应用
{
T型;
}
};
使用方法:
template <int A,char B, bool C> struct Foo{};
typedef Add_true<Foo>::apply<42, '*'>::type my_type; // Foo<42, '*', true>
模板结构Foo{};
typedef Add_true::apply::type my_type;//福
继承将是C++03的方式
template< template <int A,char B,bool> typename T>
struct Add_true {
template <int A,char B>
struct type : T<A,B,true> {};
};
template
结构添加_true{
样板
结构类型:T{};
};
在C++11和C++03中,
type
是新模板的名称。区别只在于专业化意味着什么。对于别名模板,它们完全代表被别名的对象,而在这里它们是新类型。但是,派生到基的转换应该允许将C++03类型
专门化几乎完全视为它们的“别名”。继承将是C++03的方式
template< template <int A,char B,bool> typename T>
struct Add_true {
template <int A,char B>
struct type : T<A,B,true> {};
};
template
结构添加_true{
样板
结构类型:T{};
};
在C++11和C++03中,
type
是新模板的名称。区别只在于专业化意味着什么。对于别名模板,它们完全代表被别名的对象,而在这里它们是新类型。但是,派生到基的转换应该允许将C++03类型
专门化几乎完全视为它们的“别名”。在C++03中,我们没有模板别名,但可以通过以下结构模板实现同样的效果:
template<template<int A, char B> class T> class A1
{
typedef typename T<1, 'a'>::type actual_A2;
};
template<int A, char B, bool C> class A2 {};
template< template<int A, char B, bool C> class T >
struct Add_true
{
template<int A, char B>
struct apply
{
typedef T<A, B, true> type;
};
};
typedef Add_true<A2> A2_with_true;
A1< A2_with_true::apply > a1;
模板类A1
{
typedef typename T::type实际_A2;
};
模板类A2{};
模板<模板类T>
结构添加_true
{
样板
结构应用
{
T型;
};
};
typedef将_true A2_与_true相加;
A1A1;
请注意,您需要更改
A1
的工作方式,因为它必须使用嵌套的type
typedef来获得实际的A2
专门化。在C++03中,我们没有模板别名,但类似这样的结构模板也可以实现这一点:
template<template<int A, char B> class T> class A1
{
typedef typename T<1, 'a'>::type actual_A2;
};
template<int A, char B, bool C> class A2 {};
template< template<int A, char B, bool C> class T >
struct Add_true
{
template<int A, char B>
struct apply
{
typedef T<A, B, true> type;
};
};
typedef Add_true<A2> A2_with_true;
A1< A2_with_true::apply > a1;
模板类A1
{
typedef typename T::type实际_A2;
};
模板类A2{};
模板<模板类T>
结构添加_true
{
样板
结构应用
{
T型;
};
};
typedef将_true A2_与_true相加;
A1A1;
请注意,您需要更改
A1
的工作方式,因为它必须使用嵌套的type
typedef来获得实际的A2
专门化。我不喜欢那里的继承。。。单独检索T
并不是一个简单的方法。@Quentin-是的,这就是为什么别名模板被引入并具有优越性的原因。但是如果您仅限于C++03,并且您的目标是创建一个新模板(而不是说,执行类似于std::allocator::rebind
)的操作)。那就是了。谢谢,在我接受答案之前,我必须花一些时间在这上面。我不喜欢那里的遗产。。。单独检索T
并不是一个简单的方法。@Quentin-是的,这就是为什么别名模板被引入并具有优越性的原因。但是如果您仅限于C++03,并且您的目标是创建一个新模板(而不是说,执行类似于std::allocator::rebind
)的操作)。那么就这样了。谢谢,在我接受答案之前,我必须花一些时间。对不起,你的“简化”走错了方向。如果你阅读了我链接的问答,你的A2\u with\u true
就是OP在问题中所做的,而且你的C++03解决方案也不是我想要的,因为你的A2\u和
只绑定true
到A2
,但要对Foo
或Bar
执行相同的操作,我必须编写一个Foo\u和Bar\u和
,这正是要避免的,这一点从问题上看不清楚,但是我把答案更新为defineAdd\u true
。我对另一个问题也有同样的疑问。现在,您传递给A1
的参数不是类型本身,而是A1
内部的参数。我必须使用T::type
来获得所需的类型,不是吗?很抱歉,您的“简化”方向错误。如果你阅读了我链接的问答,你的A2\u with\u true
就是OP在问题中所做的,而且你的C++03解决方案也不是我想要的,因为你的A2\u和
只绑定true
到A2
,但要对Foo
或Bar
执行相同的操作,我必须编写一个Foo\u和Bar\u和
,这正是要避免的,这一点从问题上看不清楚,但是我把答案更新为defineAdd\u true
。我对另一个问题也有同样的疑问。现在,您传递给A1
的参数不是类型本身,而是A1
内部的参数,我必须使用T::type
来获得所需的类型,否?不确定我是否理解此选项