[英]typedef default template type to same name
Original question: I have a type template <typename CostType> struct Key
which I use in another class UQueue
, which is also templated on CostType
.原始问题:我有一个类型template <typename CostType> struct Key
,我在另一个 class UQueue
中使用它,它也在CostType
上模板化。 I'd like to not have to specify Key<CostType>
within this class.我不想在这个 class 中指定Key<CostType>
。 I tried typedef Key<CostType> Key
, which did not work.我试过typedef Key<CostType> Key
,但没有用。 Is there another workaround?还有另一种解决方法吗?
Edit: The minimal example that would have exhibited the problem (if there had been one), would have been this:编辑:会出现问题的最小示例(如果有的话)是这样的:
template <typename T>
class C1 {
T t;
};
template <typename T>
class C2 {
typedef C1<T> C1;
C1 c1;
};
However, this works ( using MSVC 2010 ).但是,这有效(使用 MSVC 2010 )。 I have some other error in my code which confused me into believing the typedef was illegal.我的代码中还有其他一些错误,让我误以为 typedef 是非法的。 Sorry about the bandwidth.对不起带宽。
Use a macro.使用宏。 That's how I always do it since it's simple and easy to maintain.这就是我一直这样做的方式,因为它简单且易于维护。
Take this situation:采取这种情况:
template <typename A, size_t B, int C, pointer_to_some_func D> class tTemplate
{
struct myStruct
{
int i, j;
};
void function1 (tTemplate <A, B, C, D> :: myStruct S);
void function2 ();
};
template <typename A, size_t B, int C, pointer_to_some_func D> void tTemplate <A, B, C, D> :: function1 (tTemplate <A, B, C, D> :: myStruct S)
{
}
etc... ETC...
Imagine if I had to change an argument or add another one...想象一下,如果我必须改变一个论点或添加另一个论点......
Now, with this:现在,有了这个:
#define dTemplate tTemplate <A, B, C, D>
#define sTemplate template <typename A, size_t B, int C, pointer_to_some_func D>
sTemplate void dTemplate :: function1 (dTemplate::myStruct S)
{
}
Easier to maintain, useable by other classes\templates etc. One template argument change = a macro change which applies everywhere.更易于维护,可用于其他类\模板等。一个模板参数更改 = 适用于任何地方的宏更改。 And it is also nicer to the eyes.而且对眼睛也更好。 Also, I find it a good thing in templates, especially since typedefs are no-go.另外,我发现它在模板中是一件好事,尤其是因为 typedef 是不行的。 And best thing about macros, even IF typedefs were a standard: you don't need forward declarations for them... ever, (yeah, macros are evil, but in situations like theese, they're more than useful)关于宏最好的事情,即使 IF typedefs 是一个标准:你不需要为它们前向声明......永远,(是的,宏是邪恶的,但在这些情况下,它们不仅仅是有用的)
As for your example:至于你的例子:
#define dC1 C1 <T> // no ";" !!!
template <typename T> class C1
{
T t;
};
template <typename T> class C2
{
dC1 c1;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.