If you really really want to use this notation and the standard type traits, you should write your const
overload this way:
auto getEnergySolver() const ->
std::add_lvalue_reference<
std::add_const<decltype(PhysicalNode::energySolver_)>::type
// ^^^^^^^^^^^^^^
>::type;
Otherwise you would be returning a reference to non-const
, which is clearly wrong considering your member function is const
-qualified.
Notice, however, that type traits are not really needed here (if EnergySolver
is just a regular type and not a reference type alias):
auto getEnergySolver()-> decltype(PhysicalNode::energySolver_)&;
auto getEnergySolver() const -> decltype(PhysicalNode::energySolver_) const&;
But even decltype
is unnecessary. If your real program is not more complicated than the example you are showing, this is enough:
auto getEnergySolver()-> solver::EnergySolver&;
auto getEnergySolver() const -> solver::EnergySolver const&;