0

I would like to better understand the use of the operator && in the example below with std::string

Code:

#include <iostream>
using namespace std;

const std::string GetEditTxtAccount()
{
 std::string str = "Hello";
 return str;
}

int main()
{
   const string&& x = GetEditTxtAccount();
               ^^^
}

so why did we used the operator && in the main?

Thank you.

Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
  • 3
    Look up *rvalue references*. Also note that in this case `&&` is not an operator, similarly to how `*` is not an operator in `int *x;`. – HolyBlackCat Dec 03 '18 at 12:07
  • See r-value refs. –  Dec 03 '18 at 12:07
  • @StoryTeller: Neither of those posts looks like a duplicate to me. Am I missing something? – Lightness Races in Orbit Dec 03 '18 at 12:09
  • @HolyBlackCat : yes I know that it's related to rvalue-ref but, in this case there is no compiler optilization ? Are we needy of the `&&`. – Blood-HaZaRd Dec 03 '18 at 12:09
  • @LightnessRacesinOrbit - The OP is asking about the meaning of this funny symbol. Sure looks like a dupe to me. – StoryTeller - Unslander Monica Dec 03 '18 at 12:10
  • 1
    @StoryTeller That's a really broad definition of "duplicate" unless we want to close _every_ question that asks _anything_ about rvalue refs as dupes of questions asking about rvalue refs as function arguments. This is not that. In particular I can't see an answer to _this_ question anywhere on either page. – Lightness Races in Orbit Dec 03 '18 at 12:10
  • @LightnessRacesinOrbit - That's not "anything". If you read something different than me in this question, I think we can surmise it's at best unclear, if not a duplicate. – StoryTeller - Unslander Monica Dec 03 '18 at 12:11
  • @StoryTeller To me the question is pretty clear and does not have anything close to an answer on those pages, but I'd be happy to be proven wrong! This is about binding an rvalue ref to the value returned from a function, and why the author might have done it. – Lightness Races in Orbit Dec 03 '18 at 12:12
  • @LightnessRacesinOrbit - If the OP confirms your interpretation and agrees to reword the question to make it clearer, I'd be happy to re-open too. – StoryTeller - Unslander Monica Dec 03 '18 at 12:13
  • @StoryTeller I'm just struggling to see how you're finding this unclear and confusing it with rvalue refs as function arguments. That's a completely different thing to this. Hopefully someone else can come along and tell us. – Lightness Races in Orbit Dec 03 '18 at 12:14
  • @LightnessRacesinOrbit - The very first sentence in the question looks like a vanilla "what is this declaration with && is" to me. If this is about why you'd use a const rvalue reference, then I think there's a dupe for that too. – StoryTeller - Unslander Monica Dec 03 '18 at 12:15
  • 1
    @StoryTeller The trick is to read the _whole_ question, not just the first sentence.... and even if we limit ourselves to the first sentence, to read it properly. For example, the phrase "in the example below" probably really does mean "in the example below", not "in an example of unrelated, different code on another question posted in the past". :) – Lightness Races in Orbit Dec 03 '18 at 12:16
  • @StoryTeller : I won't say that I did not look for an answer before postinh the quest, in fact as LightnessRacesinOrbit said, I did not find a one that answers my case. – Blood-HaZaRd Dec 03 '18 at 12:17

1 Answers1

7

This code is storing an rvalue reference to the new string, relying on lifetime extension to keep the actual string alive.

In this case it's pretty much like doing const string& x = GetEditTxtAccount(), which is also pointless.

It's also arguably dangerous because if the function returned a reference then you'd potentially have it dangling.

Certainly there is no benefit in doing this.

Just declare a proper value instead:

const string x = GetEditTxtAccount();

If you're worried about copies, you won't get one, because of move semantics (pre-C++17) and because of guaranteed elision (since C++17).

As for why the author wrote it this way, well, some people go over the top with rvalue refs without really understanding why.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055