Based on this topic, I wonder if it would be possible to have a factory class that would provide a container type, without defining the type of element of the container.
Something like:
template <typename t_container>
class factory {
public:
using container = t_container;
}
so that something like this, but not exactly with this syntax because I know it is not valid in C++, would work:
...
factory<std::vector>::container<int> my_container;
...
The idea is that factory would define the type of container, but not the type of element that factory::container would contain, leaving that decision to the user code.
You want
t_containerto be a template, hencetemplate <typename t_container>is wrong (it declarest_containerto be a type, butstd::vectoris not a type). And after fixing that, yourusingdeclaration isnt right, because it assumest_containeris a type.You can do that by using a template template argument:
Live Demo
The same
factory<std::vector>can make vectors with different element type as illustrated by the member alias template and member function template.There is only one small issue, when using container with non-type template arguments. The above will not work for
std::array, because its 2nd argument is asize_tnot a type. Also egstd::mapis not covered by the above, because its element type is specified by 2 parameter (key and value type).