views:

123

answers:

2

I am trying to do something like this:

template <typename T,bool Strong=true>
class Pointer {...};

template <typename T>
typedef Pointer<T,false> WeakPointer;

But this is a compile error ("a typedef template is illegal" VC).

I am trying to avoid doing this using inheritance, beacuse that's more unnecessary work (rewriting constructors, operator =, back-and-forth casting, friendship...).

Any ideas?

A: 

C++03 doesn't support templated typedefs. You'd have to specify both types:

typedef Pointer<int,false> WeakIntPointer;

I know that isn't very helpful, but it's a reality of the language. Luckily, C++0x will support template typedefs.

For now, you'd really have to make a templated WeakPointer class which takes a template parameter to indicate the type.

Charles Salvia
+5  A: 

C++0x will alleviate this issue, but as it stands you cannot.

The common work-around is this:

template <typename T,bool Strong=true>
class Pointer {...};

template <typename T>
struct WeakPointer
{
    typedef Pointer<T,false> value_type;
};

So instead of:

typedef WeakPointer<int> WeakInt;

You get:

typedef WeakPointer<int>::value_type WeakInt;
GMan