1

Here is a class with two getters with different return type:

class A {
    std::string m_test { "test" };

public:
    std::string test_by_value { return m_test; }
    const std::string& test_by_const_ref() { return m_test; }
};

// ...

Which is better? It is about std::string, not built-in types. Is S.T.L. says in https://channel9.msdn.com/Events/GoingNative/2013/Don-t-Help-the-Compiler that it is better to return by value, because multiple copies will be optimized? Or am I incorrectly understanded him?

DevSolar
  • 67,862
  • 21
  • 134
  • 209
vladon
  • 8,158
  • 2
  • 47
  • 91
  • 4
    https://stackoverflow.com/questions/10231349/are-the-days-of-passing-const-stdstring-as-a-parameter-over – Cory Kramer Jul 14 '15 at 12:29
  • Do you want a copy or a reference? That should answer your question for you. – RamblingMad Jul 14 '15 at 12:30
  • 1
    Better for what? Design? Performance? If so, memory or time? – edmz Jul 14 '15 at 12:31
  • 1
    **1.** Return by `const reference&` if the `A` object will outlive the reference's scope and you need it readonly. **2.** If the `A` object gets out of scope and/or you need to copy/modify it, return a `value`. **3.** If you need to modify the original value return, while it stays in scope, `reference&`. --- **Don't return reference then make a copy to modify it, always return value for write access.** – CodeAngry Jul 14 '15 at 12:35
  • @CodeAngry It is not class thing to know how the rest of the program will use it. Returning by reference is the best way, because someone who use the class can decide to use a reference or make copy of the variable. – Piotr Siupa Jul 14 '15 at 12:43
  • You might want to consider that a caller of `test_by_const_ref()` could `const_cast<>` the returned reference, and then proceed to modify your (private) `m_test`. This is not possible through `test_by_value()`. – DevSolar Jul 14 '15 at 12:47
  • @DevSolar If someone want to spoil the code, he can do so regardless of how much protection you come up. – Piotr Siupa Jul 14 '15 at 12:52
  • @NO_NAME: That is correct, but I don't have to deliberately set traps for the client either. See Maxim's answer, for example. There are *very* few occassions where I *really* want a const reference out of a getter. – DevSolar Jul 14 '15 at 13:00

3 Answers3

5

By value.

I encountered similar code in the wild:

A foo();

std::string const& x = foo().test_by_const_ref();

Boom, x is a dangling reference.

That does not happen with returns by value.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
5

The link is correct, return string objects by value. NRVO will take care of returning references instead behind the scenes and your code will be perfectly semantic and clean as a result.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • This implies that he wants a copy of the string. He may want to just examine the string. – RamblingMad Jul 14 '15 at 12:35
  • You're misreading it then. – Blindy Jul 14 '15 at 12:54
  • As far as I know, NRVO is used to optimize local variables, not member variables. I doubt it will help in this case. More here: https://stackoverflow.com/questions/35308689/does-the-compiler-perform-return-value-optimisation-in-case-of-returning-member. – mentalmushroom Apr 06 '23 at 11:39
2

Return by value , since compiler will optimise return values.

mystic_coder
  • 462
  • 2
  • 10