Template as template argument for template and this is going to work - why?

Template as template argument for template and this is going to work - why?

Post by Roman Yank » Mon, 06 Sep 2004 02:46:55


Here is the code:


template <typename T> struct S1
{
T type;
};
template <template <class> class T>
class C1
{
};
typedef C1<S1> MyC1;
int main()
{
return 0;
}


There is no type specification for "S1::type" and still the code compiles?!!
can someone explain what type is going to be used by S1 to declare its
property?

Also what does "template <class>" means?

Frankly I don't understand the code above...

thanks for your explanations.

Roman.
 
 
 

Template as template argument for template and this is going to work - why?

Post by Doug Harri » Mon, 06 Sep 2004 03:59:09


It's up to C1 to instantiate a specialization of its template template
parameter T. It does not, so there is no instantiated S1<???>::type in the
program above; indeed, there is no instantiated S1 in the program above.


It introduces a template template parameter, i.e. a template parameter that
is itself a template.

--
Doug Harrison
Microsoft MVP - Visual C++

 
 
 

Template as template argument for template and this is going to work - why?

Post by Roman Yank » Mon, 06 Sep 2004 08:47:42

>It introduces a template template parameter, i.e. a template parameter that



So this is a standardized way to allow template template parameter, right?



I have also tried replacing the following line:

template <template <class> class T> class C1
with this one
template < template <typename U> class V > class C1



Everything was compiled without errors.



Can you please tell me what is the difference between them or the 1st line
is just a short equivalent of the second?



Roman.







"Doug Harrison [MVP]" < XXXX@XXXXX.COM > ???????/???????? ? ???????? ?????????:


compiles?!!
that
 
 
 

Template as template argument for template and this is going to work - why?

Post by Carl Danie » Mon, 06 Sep 2004 13:47:54


They are identical in meaning.

Note that:

template < template < class > typename T > class V {};

would be an error.

-cd
 
 
 

Template as template argument for template and this is going to work - why?

Post by Roman Yank » Mon, 06 Sep 2004 18:22:53

Okey I got it :) Thanks a lot, only one more little question:

After i have defined type MyC1 (typedef C1<S1> MyC1;)

How do I pass a parameter into that template? The reason I ask is in the
code below.
The following code compiles fine:

MyC1 mc1;

but not that one:

MyC1<B> mc1;//B is a class or struct



"Carl Daniel [VC++ MVP]" < XXXX@XXXXX.COM >


> Roman Yankin wrote> >> > I have also tried replacing the following line> >> > >> > templa<e clas> T> class C> >> > with this on> >> > templa<e < templa<e class>V > class C>
> They are identical in meaning>
> Note that>
> templa<e < templa<e < cla>s > typename>T > class V {}>
> would be an error>
> -c>
>
 
 
 

Template as template argument for template and this is going to work - why?

Post by Carl Danie » Mon, 06 Sep 2004 23:43:00


That shouldn't compile, unless I'm missing something.


If this compiles, then MyC1 is a type, not a template...


... in which case adding template arguments to MyC1 is invalid since it's
not a template.

I'd suggest re-posting a more complete example so we're all on the same
page.

-cd
 
 
 

Template as template argument for template and this is going to work - why?

Post by Doug Harri » Tue, 07 Sep 2004 05:48:32


MyC1 is not a template. Let me expand on what I said earlier:

<q>
It's up to C1 to instantiate a specialization of its template template
parameter T. It does not, so there is no instantiated S1<???>::type in the
program above; indeed, there is no instantiated S1 in the program above.
</q>

The specialization C1<S1> (for which MyC1 is a typedef) could (and typically
would) instantiate a specialization of its template parameter S1, but
there's nothing more for the C1<S1> user to do. You plugged a template into
C1's template template parameter, so you're done, and now it's up to C1 to
use it.

--
Doug Harrison
Microsoft MVP - Visual C++
 
 
 

Template as template argument for template and this is going to work - why?

Post by Doug Harri » Tue, 07 Sep 2004 05:48:32


Yes.


The two forms you've shown are equivalent, in the same way the following all
declare the same function:

void f(int);
void f(int x);
void f(int y);

Here, the parameter names convey no useful information to the compiler, as
the parameter is named (or not) in the function definition. The names used
in these declarations are ignored, but they can be used for documentation
purposes as a sort of "code comment". Similarly, template parameters are
given names by template definitions, not the unbounded number of possible
declarations.

--
Doug Harrison
Microsoft MVP - Visual C++
 
 
 

Template as template argument for template and this is going to work - why?

Post by Roman Yank » Tue, 07 Sep 2004 19:18:11

> I'd suggest re-posting a more complete example so we're all on the same

The listing looks like this:

template <typename T> struct S1
{
T type;
};
template <template <typename> class T>
class C1
{
};
typedef C1<S1> MyC1;

int main(){
MyC1myc;
return 0;
}

However I think it is pretty useless to right such code as it does not give
any aditional functionality (I mean "typedef C1<S1> MyC1; MyC1myc;"),
however I may be wrong.


"Carl Daniel [VC++ MVP]" < XXXX@XXXXX.COM >


> Roman Yankin wrote> >> > Okey I got it :) Thanks a lot, only one more little question> >> > >> > After i have defined type MyC1 (typedef C1>S1> MyC1;>
> That shouldn't compile, unless I'm missing something>
> If this compiles, then MyC1 is a type, not a template..>
> ... in which case adding template arguments to MyC1 is invalid since it'>
> not a template>
> I'd suggest re-posting a more complete example so we're all on the sam>
> page>
> -c>
>
 
 
 

Template as template argument for template and this is going to work - why?

Post by Roman Yank » Tue, 07 Sep 2004 19:22:27

>You plugged a template into

Thanks Doug! This explains that such code is quite useless.

Roman.


"Doug Harrison [MVP]" < XXXX@XXXXX.COM > ???????/???????? ? ???????? ?????????:


typically
into