Connecting Tech Pros Worldwide Forums | Help | Site Map

template template argument

Fei Liu
Guest
 
Posts: n/a
#1: Jul 31 '06
#include <vector>
#include <iostream>

template <template <typename T, typename Allocclass C>
struct A{
//C<Tc;
//A(){ c[0]=10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A<std::vectora;
a.print();
}

This posted code seems the only valid way of using template template
argument in this context. I've tried a few combinations of rearranging
things but they all failed the compiler. I am wondering what's the
point of a A<std::vectora? It really cannot contain anything, can it?

Thanks,


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 31 '06

re: template template argument


Fei Liu wrote:
Quote:
#include <vector>
#include <iostream>
>
template <template <typename T, typename Allocclass C>
struct A{
//C<Tc;
//A(){ c[0]=10; }
void print(){ std::cout << "A" << std::endl; }
};
>
int main(){
A<std::vectora;
a.print();
}
>
This posted code seems the only valid way of using template template
argument in this context. I've tried a few combinations of rearranging
things but they all failed the compiler.
Not that it's necessary, but if you're wondering about the reasons, you
should consider posting the failed variations and compiler diagnostics.
Then somebody could explain...
Quote:
I am wondering what's the
point of a A<std::vectora? It really cannot contain anything, can
it?
Well, here it can't. But chang it to

template<class T, template<class,classclass C...

and uncomment the C<Tthing, make a couple of changes, and you might
actually get a working wrapper around a standard container.

I am guessing that what you have here is just an example of how to use
template template argument. Nothing more, really. Probably isn't in
any way intended to be useful.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Fei Liu
Guest
 
Posts: n/a
#3: Aug 3 '06

re: template template argument



Victor Bazarov wrote:
Quote:
Fei Liu wrote:
Quote:
#include <vector>
#include <iostream>

template <template <typename T, typename Allocclass C>
struct A{
//C<Tc;
//A(){ c[0]=10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A<std::vectora;
a.print();
}

This posted code seems the only valid way of using template template
argument in this context. I've tried a few combinations of rearranging
things but they all failed the compiler.
>
Not that it's necessary, but if you're wondering about the reasons, you
should consider posting the failed variations and compiler diagnostics.
Then somebody could explain...
>
Quote:
I am wondering what's the
point of a A<std::vectora? It really cannot contain anything, can
it?
>
Well, here it can't. But chang it to
>
template<class T, template<class,classclass C...
>
and uncomment the C<Tthing, make a couple of changes, and you might
actually get a working wrapper around a standard container.
>
I am guessing that what you have here is just an example of how to use
template template argument. Nothing more, really. Probably isn't in
any way intended to be useful.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, with the hint, I am able to produce this snippet that works.
But it does seem that it's going through too much trouble to achieve
the result.

#include <vector>
#include <iostream>

template <typename T, typename Alloc, template <typename, typename>
class C>
struct A{
C<T, Allocc;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A<int, std::allocator<int>, std::vector a;
a.print();
}

Victor Bazarov
Guest
 
Posts: n/a
#4: Aug 3 '06

re: template template argument


Fei Liu wrote:
Quote:
Victor Bazarov wrote:
Quote:
>Fei Liu wrote:
Quote:
>>#include <vector>
>>#include <iostream>
>>>
>>template <template <typename T, typename Allocclass C>
>>struct A{
>> //C<Tc;
>> //A(){ c[0]=10; }
>> void print(){ std::cout << "A" << std::endl; }
>>};
>>>
>>int main(){
>> A<std::vectora;
>> a.print();
>>}
>>>
>>This posted code seems the only valid way of using template template
>>argument in this context. I've tried a few combinations of
>>rearranging things but they all failed the compiler.
>>
>Not that it's necessary, but if you're wondering about the reasons,
>you should consider posting the failed variations and compiler
>diagnostics. Then somebody could explain...
>>
Quote:
>>I am wondering what's the
>>point of a A<std::vectora? It really cannot contain anything, can
>>it?
>>
>Well, here it can't. But chang it to
>>
> template<class T, template<class,classclass C...
>>
>and uncomment the C<Tthing, make a couple of changes, and you might
>actually get a working wrapper around a standard container.
>>
>I am guessing that what you have here is just an example of how to
>use template template argument. Nothing more, really. Probably
>isn't in any way intended to be useful.
>>
>V
>--
>Please remove capital 'A's when replying by e-mail
>I do not respond to top-posted replies, please don't ask
>
Thanks, with the hint, I am able to produce this snippet that works.
But it does seem that it's going through too much trouble to achieve
the result.
>
#include <vector>
#include <iostream>
>
template <typename T, typename Alloc, template <typename, typename>
class C>
struct A{
C<T, Allocc;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};
>
int main(){
A<int, std::allocator<int>, std::vector a;
a.print();
}
If you don't care about definition the standard allocator, you could
use a "template typedef trick" (there is not template typedef yet, that
is why I use the quotes):

#include <vector>
template<class Tstruct use_vector { typedef std::vector<Ttype; };

#include <list>
template<class Tstruct use_list { typedef std::list<Ttype; };

#include <iostream>
#include <typeinfo>

template<class T, template<classclass Cstruct A {
typename C<T>::type c;
A() { c.push_back(10); }
void print() {
std::cout << "A<" << typeid(T).name()
<< ',' << typeid(c).name() << " >\n"; }
};

int main() {
A<int, use_vectora;
A<double, use_listd;
a.print();
d.print();
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Fei Liu
Guest
 
Posts: n/a
#5: Aug 3 '06

re: template template argument



Fei Liu wrote:
Quote:
Victor Bazarov wrote:
Quote:
Fei Liu wrote:
Quote:
#include <vector>
#include <iostream>
>
template <template <typename T, typename Allocclass C>
struct A{
//C<Tc;
//A(){ c[0]=10; }
void print(){ std::cout << "A" << std::endl; }
};
>
int main(){
A<std::vectora;
a.print();
}
>
This posted code seems the only valid way of using template template
argument in this context. I've tried a few combinations of rearranging
things but they all failed the compiler.
Not that it's necessary, but if you're wondering about the reasons, you
should consider posting the failed variations and compiler diagnostics.
Then somebody could explain...
Quote:
I am wondering what's the
point of a A<std::vectora? It really cannot contain anything, can
it?
Well, here it can't. But chang it to

template<class T, template<class,classclass C...

and uncomment the C<Tthing, make a couple of changes, and you might
actually get a working wrapper around a standard container.

I am guessing that what you have here is just an example of how to use
template template argument. Nothing more, really. Probably isn't in
any way intended to be useful.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>
Thanks, with the hint, I am able to produce this snippet that works.
But it does seem that it's going through too much trouble to achieve
the result.
>
#include <vector>
#include <iostream>
>
template <typename T, typename Alloc, template <typename, typename>
class C>
struct A{
C<T, Allocc;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};
>
int main(){
A<int, std::allocator<int>, std::vector a;
a.print();
}
The updated version is even better to illustrate the point of template
template argument, to coordinate template arguments so that one does
not write
A<int, std::allocator<double>, std::vectora and get confused with
compiler errors. Well to be honest it could be very confusing anyway.

#include <vector>
#include <iostream>

template <typename T, template <typenameclass Alloc, template
<typename, typenameclass C>
struct A{
C<T, Alloc<T c;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};

int main(){
A<int, std::allocator, std::vector a;
a.print();
}

Closed Thread