I want to overload a method, based only on template argument (which is a type), without passing any method arguments. For that, I use code like this (simplified, of course) in C++17:
template<typename T>
class C {
public:
auto f() const {
return f((T*) nullptr);
}
private:
int f(int *) const {
return 42;
}
std::string f(std::string *) const {
return "hello";
}
std::vector<char> f(std::vector<char> *) const {
return {'h', 'i'};
}
};
dump(C<int>().f());
dump(C<std::string>().f());
dump(C<std::vector<char>>().f());
, which outputs:
42
"hello"
std::vector{'h', 'i'}
I am not a professional C++ developer, and this way looks hack-ish to me. Is there a better way to overload method f
without using pointers as helper arguments? Or is this a "normal" solution?
P.S.: I'm not bonded to any particular C++ version.