I’d like the child class within CRTP to define the element type of a vector in the parent class.
However, I’d prefer the child type to be accessed via the CHILD
parameter, rather than being passed in as another template parameter. This is because in reality the type is more complicated.
Unfortunately the below won’t compile, presumably because the Child isn’t complete at the time Parent accesses CHILD::x
.
#include <vector>
template<class CHILD>
struct Parent
{
std::vector<typename CHILD::x> vec; // Prefer element type defined through CHILD parameter
};
struct Child : public Parent<Child>
{
using x = int; // In reality this type isn't an int, it's more complicated
};
int main()
{
Child c;
return 0;
}
3