template template argument

F

Fei Liu

#include <vector>
#include <iostream>

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

int main(){
A<std::vector> a;
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::vector> a? It really cannot contain anything, can it?

Thanks,
 
V

Victor Bazarov

Fei said:
#include <vector>
#include <iostream>

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

int main(){
A<std::vector> a;
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...
I am wondering what's the
point of a A<std::vector> a? It really cannot contain anything, can
it?

Well, here it can't. But chang it to

template<class T, template<class,class> class C> ...

and uncomment the C<T> thing, 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
 
F

Fei Liu

Victor said:
Fei said:
#include <vector>
#include <iostream>

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

int main(){
A<std::vector> a;
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...
I am wondering what's the
point of a A<std::vector> a? It really cannot contain anything, can
it?

Well, here it can't. But chang it to

template<class T, template<class,class> class C> ...

and uncomment the C<T> thing, 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

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, Alloc> c;
A(){ c[0] = 10; }
void print(){ std::cout << "A" << std::endl; }
};

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

Victor Bazarov

Fei said:
Victor said:
Fei said:
#include <vector>
#include <iostream>

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

int main(){
A<std::vector> a;
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...
I am wondering what's the
point of a A<std::vector> a? It really cannot contain anything, can
it?

Well, here it can't. But chang it to

template<class T, template<class,class> class C> ...

and uncomment the C<T> thing, 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

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, Alloc> c;
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 T> struct use_vector { typedef std::vector<T> type; };

#include <list>
template<class T> struct use_list { typedef std::list<T> type; };

#include <iostream>
#include <typeinfo>

template<class T, template<class> class C> struct 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_vector> a;
A<double, use_list> d;
a.print();
d.print();
}

V
 
F

Fei Liu

Fei said:
Victor said:
Fei said:
#include <vector>
#include <iostream>

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

int main(){
A<std::vector> a;
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...
I am wondering what's the
point of a A<std::vector> a? It really cannot contain anything, can
it?

Well, here it can't. But chang it to

template<class T, template<class,class> class C> ...

and uncomment the C<T> thing, 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

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, Alloc> c;
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::vector> a and get confused with
compiler errors. Well to be honest it could be very confusing anyway.

#include <vector>
#include <iostream>

template <typename T, template <typename> class Alloc, template
<typename, typename> class 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();
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top