I'd like the following piece of code to work:
template <typename Self>
struct foo_base {
auto get(typename Self::type n) { return n; }
};
template <typename T>
struct foo : public foo_base<foo<T>> {
using type = T;
};
The problem of course is that the base is instantiated first so you cannot refer to the derived member types. I'd need some kind of lazy-evaluation here.
I've tried to make the function template and have SFINAE on it, something like:
template <typename Self>
struct foo_base {
template <typename T, typename = std::enable_if_t<std::is_same_v<T, typename Self::type>>>
auto get(T n) { return n; }
};
but it doesn't seem to affect the order. Any ideas?
Edit:
Constraints of solution:
- I can't pass the type as a template parameter from the derived class.
Main reason is: The type is very complicated to construct, several
hundred characters. So can't do something like
struct foo : foo_base<foo<T>, T>
or variants. - I need to constraint the function to that type, I can't check inside the function. Maybe there are overloads in the derived class.