Is there an elegant way to achieve the same without manually implementing the specializations?
Starting from C++17, if constexpr
template <typename T>
T const & get() const
{
if constexpr ( std::is_same_v<T, C1> )
return c1;
else if constexpr ( std::is_same_v<T, C2> )
return c2;
else if constexpr ( std::is_same_v<T, C3> )
return c3;
}
Before C++17... the best I can imagine is the creation of a std::tuple<C1, C2, C3>
and the return of the std::get<T>(tuple)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…