Function templates can't be partially specialized.
Your options are:
- One big function with a bunch of
if constexpr
in it. - Overloading the function instead of specializing.
- Wrapping the function in a class template (making the function itself non-template), and specializing the whole class. You can then provide a function that calls into it, for a nicer syntax.
I'd go with (3).
For (2), to make the "any derived class" overload, any form of SFINAE will work, e.g.:
template <std::derived_from<MyStringBase> DerivedString>DerivedString GetValue<DerivedString>(const TObject &obj, const std::string &field) {...}
For int
and float
, I'd perhaps replace specializations with overloads too, just to be consistent:
template <std::same_as<int>>int GetValue(const TObject& obj, const std::string& field);