I want to create a template class that can hold any combination of container and containee. For example, a std::vector<std::string>
or std::map<std::tree>
, for instance.
I have tried a lot of combinations, but I must admit that the complexity of templates overwhelms me. The closes I have made compile is something like this:
template <class Vector, template <typename, class Containee = std::string> class Container>
class GenericContainer
{
Container<Containee> mLemario;
};
Although it compiles so far, then, when I want to instantiate it I rreceive lots of errors.
MyContainer<std::vector, std::string> myContainer;
Am I using the correct approach to create that kind of class?
For std::vector
(and the like) @songyuanyao provided an excellent answer. But since you also mentioned std::map
, I'll add a simple extension of @songyuanyao's answer, online.
#include <iostream>
#include <vector>
#include <string>
#include <map>
template <template <typename...> class Container, typename Containee = std::string, typename... extras>
class GenericContainer
{
Container<Containee, extras ...> mLemario;
// Use 'Containee' here (if needed) like sizeof(Containee)
// or have another member variable like: Containee& my_ref.
};
int main()
{
GenericContainer<std::vector, std::string> myContainer1;
GenericContainer<std::vector, std::string, std::allocator<std::string>> myContainer2; // Explicitly using std::allocator<std::string>
GenericContainer<std::map, std::string, int> myContainer3; // Map: Key = std::string, Value = int
}
I want to create a template class that can hold any combination of container and containee
You should use parameter pack for template template parameter Container
, and Containee
, then they could be used with arbitrary number/type of template parameters. e.g.
template <template <typename...> class Container, typename... Containee>
class GenericContainer
{
Container<Containee...> mLemario;
};
then
GenericContainer<std::vector, std::string> myContainer1;
GenericContainer<std::map, std::string, int> myContainer2;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With