Шаблон функции-члена не может быть объявлен как virtual
, поэтому вы можете изменить объявление основного шаблона на
class Example2 {
public:
template <class T> T get();
};
, а затем специализировать его как
template <>
float Example2::get<float>(){
return 0;
}
template <>
int Example2::get<int>(){
return 0;
}
template <>
std::vector<float> Example2::get<std::vector<float>>(){
return std::vector<float>();
}
, так как C ++ 17 мы можем использовать constexpr, если как
class Example2 {
public:
template <class T> T get();
};
template <typename T>
T Example2::get() {
if constexpr (std::is_same_v<T, float>)
return 0;
else if constexpr (std::is_same_v<T, int>)
return 0;
else if constexpr (std::is_same_v<T, std::vector<float>>)
return std::vector<float>();
else
return ...;
}