1) Non-const references can only be bound to l-values (variables and the like). Const references accept r-values.
2) Implicit conversions produce r-values (temporaries).
3) Passing a C-style string to a function that expects
string& parameter requires conversion.
You have 2 and 3 and non-const reference parameter, which means that you violate 1.
Other than that, passing STL strings by const reference in your case is complete overkill, since you immediately create a local copy. You could pass the argument by value and then the replica would be created for you during the call and you would not need the local variable. Like this:
1 2 3 4 5
|
string foo_string(string in_string)
{
in_string.insert(3,"!!");
return in_string;
}
| |
Also, the string class is frequently implemented using the copy-on-write idiom, meaning that passing by const reference may turn out to be even less efficient.
There is a somewhat obscure wikipedia entry (
http://en.wikipedia.org/wiki/Copy-on-write). I'm sure you can google something better.
Regards