I have a class that looks like this:
template <typename T> struct myclass;
template <typename T> struct myclass {
T value;
vector< unique_ptr<myclass<T>> > children;
};
This works great, but what I'd really like is to be able to specify vector as a template parameter:
template <typename T, typename Container> struct myclass;
template <typename T, typename Container> struct myclass {
T value;
Container< unique_ptr<myclass<T>> > children;
}
myclass<int, vector> x;
I understand that this doesn't work, how do I do something like it where Container can be any vector-like class?
Sure, those are called template-templates. It's a mouthful of a name, but the syntax is roughly what you want it to be.
template <typename T, template <typename> typename Container>
struct MyClass {
T value;
Container< unique_ptr<MyClass<T, Container>> > children;
}
MyClass<int, std::vector> x;
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