I have two functions:
int func(const std::string& str) { return func(str.c_str()); }
int func(const char* str) { /* ... */ }
If I call func(std::string("abcdef"));
first function (const std::string&
) call recursively itself, not a const char*
because type conversion.
I can use Forward Declaration:
int func(const char* str);
int func(const std::string& str) { return func(str.c_str()); }
int func(const char* str) { /* ... */ }
or change functions definitions order:
int func(const char* str) { /* ... */ }
int func(const std::string& str) { return func(str.c_str()); }
and it works.
But is there another way for Overload Resolution?
CodePudding user response:
To exclude implicit type conversions from overload resolution you should make template functions that only accept exact type matches. (SFINAE) e.g.
template<typename type_t>
auto func(const type_t& string)
-> std::enable_if_t<std::is_same_v<std::string,type_t>,int>
{
}
This will resolve to a function returning an int only when std::string and type_t exactly match (without conversions)