Like many people I'm in the habit of writing new string functions as functions of const std::string &
. The advantages are efficiency (you can pass existing std::string
objects without incurring overhead for copying/moving) and flexibility/readability (if all you have is a const char *
you can just pass it and have the construction done implicitly, without cluttering up your code with an explicit std::string
construction):
#include <string>
#include <iostream>
unsigned int LengthOfStringlikeObject(const std::string & s)
{
return s.length();
}
int main(int argc, const char * argv[])
{
unsigned int n = LengthOfStringlikeObject(argv[0]);
std::cout << "'" << argv[0] << "' has " << n << " characters\n";
}
My aim is to write efficient cross-platform code that can handle long strings efficiently. My question is, what happens during the implicit construction? Are there any guarantees that the string will not be copied? It strikes me that, because everything is const
, copying is not necessary—a thin STL wrapper around the existing pointer is all that's needed—but I'm not sure how compiler- and platform-dependent I should expect that behavior to be. Would it be safer to always explicitly write two versions of the function, one for const std::string &
and one for const char *
?