template <class ContainerType, class elementType>
void SerializeContainer ( ContainerType< elementType > container )
{
}
//call like this
std::vector<int> vector;
SerializeContainer(vector);
below will not compile.. Is there any way I can get through this?
It is easier to use the
value_typemember of the container to extract the element type:The reason why your code does not work is because the syntax
ContainerType<ElementType>only works whenContainerTypeis a really template. C++ support the template-template parameter for this:But even this will not match for standard C++ containers, because there are some hidden default arguments. When written explicitly:
So you’ll need to make the ContainerType parameter to accept 2 arguments:
But then this won’t match
set::setbecause it has a different number of template parameters:There simply isn’t a one-size-fit-all solution (before C++11) if you want the ElementType spelled explicitly when matching. It’s better to stick with the traits provided by the container.