Ben
Ben

Reputation: 7628

template default argument in a template

I am trying to compile this :

template <class T, class U = myDefaultUClass<T> >
class myClass{
 ...
};

Although it seems quite intuitive to me it is not for my compiler, does anyone knows how to do this ?

edit : Ok, the problem was not actually coming from this but from a residual try ... Sorry about this, thanks for your answers anyway.

Upvotes: -1

Views: 795

Answers (4)

anon
anon

Reputation:

The following works for me using g++. Please post more code, the error messages you are getting and the compiler version.

class A {};

template <class T> class T1 {};

template <class T, class U = T1<T> > class T2 {
};

T2 <A> t2;

Upvotes: 5

Lawand
Lawand

Reputation: 1112

It's either that your compiler isn't standard complaint, or you made one of these mistakes:

  1. myDefaultUClass is not a template
  2. myDefaultUClass isn't defined

because the following works fine in G++:

class myDefaultUClass{};

template <class T, class U = myDefaultUClass >
class myClass{
 //...
};

Upvotes: 0

Klaim
Klaim

Reputation: 69792

This works on MSVC 9.0 :

template < class T >
class UClass
{
private:
    T m_data;
};

template < class T, class U = UClass< T > >
class MyClass 
{
public:
    const U& data() const { return m_data; }
private:

    U m_data;
};


int main()
{
    MyClass< int > test;

    const UClass<int>& u = test.data();

    return 0;
}

Upvotes: 0

Assaf Lavie
Assaf Lavie

Reputation: 76133

Compiles fine with Comeau...

Upvotes: 3

Related Questions