What is the best way to return string in C++?

C++String

C++ Problem Overview


My question is simple: if I have some class Man and I want to define member function that returns man's name, which of the following two variants shall I prefer?

First:

string name();

Second:

void name(/* OUT */ string &name);

The first variant is kind of inefficient because it makes unnecessary copies (local variable -> return value -> variable in the left part of assignment).

The second variant looks pretty efficient but it makes me cry to write

string name;
john.name(name);

instead of simple

string name(john.name());

So, what variant shall I prefer and what is the proper trade-off between efficiency and convenience/readability?

Thanks in advance.

C++ Solutions


Solution 1 - C++

It's a good question and the fact that you're asking it shows that you're paying attention to your code. However, the good news is that in this particular case, there's an easy way out.

The first, clean method is the correct way of doing it. The compiler will eliminate unnecessary copies, in most cases (usually where it makes sense).

EDIT (6/25/2016)

Unfortunately it seems that David Abaraham's site has been offline for a few years now and that article has been lost to the ethers (no archive.org copy available). I have taken the liberty of uploading my local copy as a PDF for archival purposes, and it can be found here.

Solution 2 - C++

use the first variant:

string name();

The compiler will most likely optimize out any unnecessary copies. See return value optimization.

In C++11, move semantics means that you don't perform a full copy even if the compiler doesn't perform RVO. See move semantics.

Also bear in mind that if the alternative is

void name(std::string& s);

then you have to specify very clearly what can happen to s and what values it can have when passed to the function, and probably either do a lot of validity checking, or simply overwrite the input entirely.

Solution 3 - C++

Since you want to create a getter for a field of your class, maybe you should go like that: inline const std::string& name() const { return this->name; }

Since the name is returned as a const reference, it won't be modified outside the class, also no copy will be created by returning the name.

After that, if you want to manipulate the name you will have to do a copy.

Solution 4 - C++

I would go with the first. Return value optimization and C++11 will remove any copying overhead.

Solution 5 - C++

As we have move-semantics (in C++11), you can use this:

string name();

Even in C++03, this is almost good, as the compiler might optimize this (Search for Return Value Optimization).

Solution 6 - C++

Rule #1 of optimization: Measure, optimize, measure. Or, as Knuth said, "premature optimization is the root of all evil".

Unless you have a strong indication that simply returning std::string will significantly impact the performance of your software, just do so. If you can measure a significant impact, find the critical path, and optimize that. Don't make any funny, project-wide "optimizations" that likely result in little to no performance benefit, but negatively impact the readability, maintainability and robustness of your code.

Solution 7 - C++

I think you should use first variant. Because this is simple getter method and such getter/setter approach is used everywhere.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontonytonyView Question on Stackoverflow
Solution 1 - C++Mahmoud Al-QudsiView Answer on Stackoverflow
Solution 2 - C++juanchopanzaView Answer on Stackoverflow
Solution 3 - C++MesopView Answer on Stackoverflow
Solution 4 - C++flumpbView Answer on Stackoverflow
Solution 5 - C++NawazView Answer on Stackoverflow
Solution 6 - C++DevSolarView Answer on Stackoverflow
Solution 7 - C++RobertView Answer on Stackoverflow