I need to write a library that contains a function that takes two string parameters:
void foo(const std::string& arg1, const std::string& arg2);
My library will be used by some people who do not like C ++ and are used only for const char* .
To satisfy my likes, I changed the prototype:
void foo(const char* arg1, const char* arg2);
And made my first version a simple inline call:
inline void foo(const std::string& arg1, const std::string& arg2) { foo(arg1.c_str(), arg2.c_str()); }
Of course, thanks to the std::string constructors, it would work almost the same way as with the first version. Writing this overload simply avoids creating a useless std::string if someone passes only const char* .
But now I wonder: is this overload really necessary or is it just a premature optimization?
In addition, I feel that this is somehow incomplete: should I also write void foo(const char* arg1, const std::string& arg2) and void foo(const std::string& arg1, const char* arg2) overloads? What if I have options 3, 4 or more ( n )? Should I write 2 n overloads?
In short, have you ever encountered a similar situation? What choice did you make and why?
Thanks.
source share