sold
sold

Reputation: 2091

Partially defaulting template arguments using typedefs?

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?

Upvotes: 1

Views: 946

Answers (2)

GManNickG
GManNickG

Reputation: 503913

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;

Upvotes: 7

Charles Salvia
Charles Salvia

Reputation: 53289

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.

Upvotes: 2

Related Questions