I’m wondering whether the C++ string is considered small enough to be more efficient when passed by value than by reference.
I’m wondering whether the C++ string is considered small enough to be more efficient
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. Pass it by reference:
In general, pass things by-reference if they have a non-trivial copy-constructor, otherwise by-value.
A string usually consists of a pointer to data, and a length counter. It may contain more or less, since it’s implementation defined, but it’s highly unlikely your implementation only uses one pointer.
In template code, you may as well use
const T&, since the definition of the function will be available to the compiler. This means it can decide if it should be a reference or not for you. (I think)