Reputation: 1101
Why doesn't this work?
template <typename T, typename U>
class TSContainer {
private:
U<T> container;
};
called as:
TSContainer<int, std::vector> ts;
I think a solution might be:
template <typename T, template <typename> typename C>
class TSContainer
{
C<T> container;
};
but that doesn't work either.
Upvotes: 2
Views: 145
Reputation: 27233
This is what you need:
#include <vector>
template <typename T, template<typename, typename> class C>
class TSContainer
{
C<T, std::allocator<T> > container;
};
int main() {
TSContainer<int, std::vector> a;
}
Note that std::vector
takes two template parameters with the second being defaulted to std::allocator
. Alternatively, you can write:
#include <vector>
template <typename T, template<typename, typename = std::allocator<T> > class C>
class TSContainer
{
C<T> container;
};
int main() {
TSContainer<int, std::vector> a;
}
Both of these force the selection of the allocator on you. If you want to control which allocator is used by your vector (i.e. what is used as second template parameter to C
), you can also use this:
#include <vector>
template <typename T, template<typename, typename> class C, typename A = std::allocator<T> >
class TSContainer
{
C<T, A> container;
};
int main() {
TSContainer<int, std::vector> a;
}
This is the most flexible solution.
Upvotes: 5
Reputation: 9425
Wouldn't it be much more easy to simply do something like:
#include <vector>
template <typename C>
class TSContainer
{
C container;
};
int main() {
TSContainer<std::vector<int> > a;
}
Upvotes: 1