vendredi 13 janvier 2023

Overload Resolution between func(const char*) and func(std::string&)

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?

Aucun commentaire:

Enregistrer un commentaire