Reputation: 2096
#include <iostream>
template <typename T>
class Container1
{
// some implementation.
};
template <typename T>
class Container2
{
// some implementation.
};
template <typename type, typename container>
class CreateContainer
{
container<type> createContainer()
{
return container<type>();
}
container<const type> createConstContainer()
{
return container<const type>();
}
};
int main()
{
// doesn't compile.
CreateContainer<int, Container1> createContainer1;
CreateContainer<int, Container2> createContainer1;
// don't wanna do that:
// CreateContainer<Container2<int>, Container2<const int>, int> createContainer1;
// I might need to know the type inside the container (the int) so it's passed as well.
}
Consider this code, I'm creating a factory class that creates a normal containers as long as containers with const elements. Thats just an example. What I'm actually doing is that I want to return an iterator to some container and a const iterator to the same container. But the most of the code is not relevant to the problem so I made up an example. I wanna pass the containers as a template argument and then specify inside the factory class what arguments to pass to the container. If I don't pass the containers without their argument list, I'll have to pass the type inside the container, the normal container type and the const container type in the argument list of the factory class which is redundant.
Upvotes: 0
Views: 1344
Reputation: 122228
You can make container
a template template parameter, and you declare createContainer1
twice in main
. This compiles:
#include <iostream>
template <typename T>
class Container1 {};
template <typename T>
class Container2 {};
template <typename type, template<class> class container>
class CreateContainer
{
container<type> createContainer()
{
return container<type>();
}
container<const type> createConstContainer()
{
return container<const type>();
}
};
int main()
{
CreateContainer<int,Container1> createContainer1;
CreateContainer<int,Container2> createContainer2;
}
Note that this assumes that the container
template parameter has a single type parameter. It wont work for eg for a template <typename T,size_t foo> struct container3 {};
, but it could be made to work, if you need that.
Upvotes: 2