simple? sequence issue

  • Thread starter Christof Warlich
  • Start date
C

Christof Warlich

Hi,

can anyone tell how to convince the compiler to accept the third
line in the example below? I need this parameter sequence because
I want to define a default argument for T , e.g.:
template<X<T> *x, typename T = int> struct Y2{};


template<typename T> struct X {};
template<typename T, X<T> *x> struct Y1{};
template<X<T> *x, typename T> struct Y2{};

Thanks for any help,

Christof
 
C

Christof Warlich

can anyone tell how to convince the compiler to accept the third
line in the example below? I need this parameter sequence because
I want to define a default argument for T , e.g.:
template<X<T> *x, typename T = int> struct Y2{};


template<typename T> struct X {};
template<typename T, X<T> *x> struct Y1{};
template<X<T> *x, typename T> struct Y2{};

Oops, just found the solution:

template<typename T> struct X {};
template<typename T, X<T> *x> struct Y1{};
class T;
template<X<T> *x, typename T> struct Y2{};

Anyway, thanks,

Christof
 
C

Christof Warlich

Christof said:
Oops, just found the solution:

Hmm - still having problems: Instantiation does not work:

template<typename T> struct X {};
template<typename T, X<T> &x> struct Y1{};
class T;
template<X<T> &x, typename T> struct Y2{};
X<int> x;
Y1<int, x> y1;
Y2<x, int> y2; // this does not compile

Anyone who knows how to do it right?

Many thanks,

Christof
 
V

Victor Bazarov

Christof said:
Hmm - still having problems: Instantiation does not work:

template<typename T> struct X {};
template<typename T, X<T> &x> struct Y1{};
class T;
template<X<T> &x, typename T> struct Y2{};
X<int> x;
Y1<int, x> y1;
Y2<x, int> y2; // this does not compile

Anyone who knows how to do it right?

When you try to instantiate Y2 template by giving it 'x' as the first
argument, the compiler has no idea what 'T' is in Y2. It cannot look
ahead to see that you've established that 'T' is 'int' in the second
argument. That's why instantiating 'Y1' works fine, since 'T' is known
at the time when it sees 'x', which then successfully matches to the
object of type X<int>.

V
 
C

Christof Warlich

template said:
When you try to instantiate Y2 template by giving it 'x' as the first
argument, the compiler has no idea what 'T' is in Y2. It cannot look
ahead to see that you've established that 'T' is 'int' in the second
argument. That's why instantiating 'Y1' works fine, since 'T' is known
at the time when it sees 'x', which then successfully matches to the
object of type X<int>.

And there is nothing like some forward declaration to tell the compiler?
As I said, I need _this_ sequence of template parameters because I'd
like to supply a default argument for T, e.g.:

template<X<T> &x, typename T = int> struct Y2{};
Y2<x> y2;

Or is there some other way to to provide this default argument?

Christof
 
V

Victor Bazarov

Christof said:
And there is nothing like some forward declaration to tell the
compiler? As I said, I need _this_ sequence of template parameters
because I'd like to supply a default argument for T, e.g.:

template<X<T> &x, typename T = int> struct Y2{};
Y2<x> y2;

Or is there some other way to to provide this default argument?

You will likely have to define a different template for that and if
you need to share functionality, derive it from the Y2:

template<X<int> &x> struct Y2int : Y2<int,X<int>&> {};

V
 
C

Christof Warlich

You will likely have to define a different template for that and if
you need to share functionality, derive it from the Y2:

template<X<int> &x> struct Y2int : Y2<int,X<int>&> {};

V
.... which doesen't seem to make things look better than abandoning the
idea of supplying a default ....
Anyhow, thanks a lot for your help, at least I know now that there is
no simple solution that I have missed. And fortunately, my life doesn't
depend on finding one ;-).

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top