I would like to use CRTP for a performance-sensitive section of my code. However, my base class has a bitset, which size depends on the derived class. I was hoping that something like that would work:
template <typename Derived>
class Base {
protected:
std::bitset<Derived::bsize> data_;
};
class Foo : public Base<Foo> {
public:
constexpr static size_t bsize = 2;
};
but the compiler complains: "no member bsize in Foo". I guess I could solve my problem by templating the bitset length too in the base class:
template <typename Derived, size_t size>
class Base {
protected:
std::bitset<size> data_;
};
class Foo : public Base<Foo,2> { ... };
In the future, I might want to have more complex expressions to compute the bitset length. Is there a way to get the job done using constexpr functions? (closer in spirit to my first non-working solution) Thanks.