 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Goncalo de Carvalho Guest
|
Posted: Thu Jun 16, 2005 8:12 am Post subject: template template parameters question |
|
|
Hey,
Consider the following code:
template<typename T>
struct D
{
};
template<typename T, template
struct A : public U<T>
{
};
template<typename T, template
= D>
struct B : public S<T, U
{
};
int main( )
{
B<unsigned char, A> a;
}
Why can't A be used above? All the compilers I tried say
"error: class template "A" is not compatible with template template
parameter "S"".
This is with Intel C++ 8.1 and VC++ .NET 2002 and 2005 beta 2.
TIA,
Goncalo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Fri Jun 17, 2005 10:11 am Post subject: Re: template template parameters question |
|
|
"Goncalo de Carvalho" <glslang_NOSPAM (AT) yah_NOSPAMoo (DOT) com> writes:
| Quote: | Hey,
Consider the following code:
template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
= D
struct B : public S<T, U
{
};
int main( )
{
B
}
Why can't A be used above?
|
The short answer is: because the standard says so.
Template arguments must match in kind and in arity.
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri Jun 17, 2005 10:21 am Post subject: Re: template template parameters question |
|
|
"Goncalo de Carvalho" <glslang_NOSPAM (AT) yah_NOSPAMoo (DOT) com> writes:
| Quote: | Hey,
Consider the following code:
template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
= D
struct B : public S<T, U
{
};
int main( )
{
B
}
Why can't A be used above?
|
Because a template is not a type. If you want S to match A, it needs to
be:
template<
typename T
, template class S
^^^^^^^^^^^^^^^^^^^^^
, template<class> class U = D>
struct B : public S<T, U
{
};
HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri Jun 17, 2005 10:22 am Post subject: Re: template template parameters question |
|
|
Hi,
Goncalo de Carvalho wrote:
| Quote: | template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
= D
|
Rather:
template<typename T, template class S,
template<class> class U = D>
| Quote: | | struct B : public S<T, U
|
Rather:
struct B : public S
| Quote: | {
};
int main( )
{
B<unsigned char, A> a;
}
|
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Fri Jun 17, 2005 10:24 am Post subject: Re: template template parameters question |
|
|
"Goncalo de Carvalho" <glslang_NOSPAM (AT) yah_NOSPAMoo (DOT) com> writes:
| Quote: | template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
= D
struct B : public S<T, U
{
};
int main( )
{
B
}
Why can't A be used above? All the compilers I tried say
"error: class template "A" is not compatible with template template
parameter "S"".
|
This is pretty much it. A is not compatible with the second parameter
of B. Why do you think it should be?
FWIW, changing the definition of B to
template<typename T,
template class S,
template<typename> class U = D>
struct B : public S<T,U>
{
};
makes the code compile.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Fri Jun 17, 2005 1:54 pm Post subject: Re: template template parameters question |
|
|
Goncalo de Carvalho wrote:
| Quote: | Hey,
Consider the following code:
template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
= D
struct B : public S<T, U
{
};
int main( )
{
B
}
Why can't A be used above? All the compilers I tried say
"error: class template "A" is not compatible with template template
parameter "S"".
This is with Intel C++ 8.1 and VC++ .NET 2002 and 2005 beta 2.
|
The sample doesn't compile because you really intended to write it like
this:
template<typename T>
struct D
{
};
template<typename T, template
struct A : public U<T>
{
};
// note that the template template parameter (S)
// itself takes a template template parameter (V)
template< typename T,
template class S,
template<class> class U = D>
struct B : public S<T, U> // second type parameter must be a
// template (U) not a type (U<T>)
{
};
Even with the sample code compiling, it does have the drawback of
being, er, complex.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Fri Jun 17, 2005 2:00 pm Post subject: Re: template template parameters question |
|
|
"Goncalo de Carvalho" <glslang_NOSPAM (AT) yah_NOSPAMoo (DOT) com> wrote
| Quote: | Hey,
Consider the following code:
template<typename T
struct D
{
};
template
struct A : public U<T
{
};
template
U
= D
struct B : public S<T, U
{
};
int main( )
{
B
}
Why can't A be used above? All the compilers I tried say
|
Because, as compiler told you, they are incompatible. Modify struct B this
way and then you get a match:
template<typename T,
template class S,
template<class> class U= D>
struct B : public S<T, U>
{
};
Gene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|