Thanks for your helpful reply - sorry for my delay in getting back.
A followup question: is there a better way to implement the CRTP when
you want to inject a static data structure into a derived class, so the
end result is that you get a new static structure for each variety of
template of the derived class you instantiate? Like so:
#include <array>
#include <iostream>
template <template <typename> class Derived, typename T>
struct Foo {
static std::array<T, 5>& my_array() {
static auto my_array = std::array<T, 5>();
return my_array;
}
std::array<T, 5>& get() {
return static_cast<Derived<T>*>(this)->get_impl();
}
};
template <typename T>
struct Bar : public Foo<Bar, T> {
std::array<T, 5>& get_impl() { return this->my_array(); }
};
int main()
{
auto baz = new Bar<int>();
auto bif = new Bar<short>();
baz->get() = {1, 2, 3, 4, 5};
bif->get() = {6, 7, 8, 9, 10};
for (auto i : baz->get())
{
std::cout << i << std::endl;
}
for (auto c : bif->get())
{
std::cout << c << std::endl;
}
return 0;
}
This works fine if you simply want to "inject" a single base class
template into the derived class, but gets real messy real quick if you
want to do a longer chain, or use multiple inheritance. Didn't know if
there's some way to automate it more cleanly...