I sit around and think about whether or not I should be using const char*
or const std::string&
pretty often when I'm designing class interfaces and when it comes down to it, I often feel like there's 6 in one hand with half a dozen in the other.
Take the following two function prototypes:
void foo(const char* str);
void foo(std::string str);
If the foo
function were to store a string, I would say the second is a better option due to the ability to pass a string and utilize move semantics where possible. However, if foo
only needed to read the string, would the const char*
solution be better?
On a performance perspective, a temporary std::string
wouldn't need to be created. However, calling the function with an already existing string as an argument looks obtrusive: foo(mystr.c_str())
. Worse yet, if more advanced operations need to be done on the array at some point down the road or if a copy should be stored, the interface then has to change.
So my questions is this:
Are there well defined, either personal or otherwise, conventions that rule when std::string
or const char*
is a better choice? Furthermore, when starting a new project, is it best to be consistent with usage or just take whichever one fits the current chunk of code best?
std::string
. – Idenvoid foo(const std::string &bar)
– Wellbredfoo("hello")
cause a temporary to be created using thestd::string
variant? – Sapienzastd::string
. It saves you so much hassle later on. Trust me. – Overnightstring_ref
-like class over either. – Orchestralstd::string
is that it can store'\0'
. – Barghest