Original question: I have a type template <typename CostType> struct Key
which I use in another class UQueue
, w开发者_C百科hich is also templated on CostType
. I'd like to not have to specify Key<CostType>
within this class. I tried typedef Key<CostType> Key
, which did not work. 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). I have some other error in my code which confused me into believing the typedef was illegal. 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 ...
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. 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)
As for your example:
#define dC1 C1 <T> // no ";" !!!
template <typename T> class C1
{
T t;
};
template <typename T> class C2
{
dC1 c1;
};
精彩评论