I want to use "static polymorphism" via CRTP to do something like the following:
template <typename T>
struct Base
{
double get_value() const { return ((T*)this)->_get_value(); }
protected:
~Base() {}
};
struct Derived1 : public Base<Derived1>
{
double value;
Derived1() : value(3.) {}
const double& _get_value() const { return value; }
};
struct Derived2 : public Base<Derived2>
{
double _get_value() const { return 5.; }
};
This works, but I'd also like that for the case of the object being instantiated as Derived1, get_value returns a const reference to the value instead of returning a copy. So in a way, a kind of "perfect forwarding" for return values.
I tried to declare get_value's return type like this:
template <typename T>
struct Base
{
decltype(std::declval<T>()._get_value()) get_value() const { return ((T*)this)->_get_value(); }
...
but unsurprisingly GCC complained that this is an invalid use of incomplete type 'struct Derived1'.
Is there any way to go around this?
Thank you in advance! :)