4

Consider the following code:

struct Foo {
    int x;    
    int foo() const & { return x; }
//                 ^^^
    int& foo() & { return x; }
//            ^^^
};

What is the purpose of the highlighted ampersands before the function body?

Danvil
  • 22,240
  • 19
  • 65
  • 88
  • 1
    This doesn't compile on VS2008. Which compiler do you use? – sashoalm May 13 '14 at 15:10
  • This: http://ideone.com/CuEeiK – Danvil May 13 '14 at 15:13
  • This seems related: http://stackoverflow.com/questions/12306226/what-does-an-ampersand-after-this-assignment-operator-mean –  May 13 '14 at 15:16
  • Related yes, but I would not say it is a duplicate. Please unmark it as such. – Danvil May 13 '14 at 15:17
  • 1
    I certainly didn't close it. Yours is different enough to warrant an answer. For one thing, yours isn't a constructor... –  May 13 '14 at 15:19
  • @Danvil Your question doesn't mention anywhere C++11, does C++11 have anything to do with this? That could explain why it doesn't compile on VS2008. – sashoalm May 13 '14 at 15:21
  • 1
    **It's certainly a dupe.** I closed it and now I've reopened it to see who wants to write an answer while a complete one is [here](http://stackoverflow.com/a/8610728/952747) & [here](http://stackoverflow.com/a/21861190/952747)! – masoud May 13 '14 at 15:22
  • @sashoalm: Apparently it is C++11 only. Edited the tags. – Danvil May 13 '14 at 15:22
  • 2
    I think the duplicate was fine, the answer is the same, no matter if you talk about an assignment operator or a member function called `foo`. – AliciaBytes May 13 '14 at 15:23
  • 4
    @Danvil how is it not a duplicate? – juanchopanza May 13 '14 at 15:24
  • 1
    @MM. You linked to the wrong question if it's a dupe... Also, the "true dupe" question is written in an entirely different format, so this question definitely looks easier to find in a search. –  May 13 '14 at 15:25
  • @ebyrob: It was right. I've read the answer in that link and it was enough to answer this current question too. Follow my new links in the comments, I've provided another links. – masoud May 13 '14 at 15:28
  • So I read through the answers you linked, but still don't know the purpose of the ampersand considering this example: `a.foo() = 5;`... – Danvil May 13 '14 at 15:28
  • 2
    @ebyrob That it's easier to find doesn't mean it shouldn't be closed as dupe. This question will keep on existing even if closed and people will be told about the other question. No need to have multiple questions with different wordings just to have all search patterns covered... – AliciaBytes May 13 '14 at 15:29
  • @MM Yes, either of your latest links is fine. The link I found was a bit different though and I would think two different questions with the same answer are merely related and not duplicate. Imagine if we marked all questions resolved by a "reboot" as duplicate! –  May 13 '14 at 15:31
  • @Danvil: This is a good question and I've upvoted it... but it doesn't mean I shouldn't flag it as duplicate when it is truly a dup. – masoud May 13 '14 at 15:32
  • 1
    @Danvil: What don't you understand? In `a.foo() = 5`, the `= 5` just follows all the usual rules of assigning to expressions. It's whether `a.foo()` itself can succeed that is the issue here. – Lightness Races in Orbit May 13 '14 at 15:51
  • 1
    @Danvil: We do not "allow" those questions! Unfortunately there are so very many of them that it is not feasible to effectively close/delete them all. Believe me, the community is trying. As for "unfriendly", we just answered your question for you, for free. What do you want? If you want "discussion" then you're definitely in the wrong place: Stack Overflow is not a discussion forum or message board, and there is absolutely no reason that it should be. – Lightness Races in Orbit May 13 '14 at 17:03
  • 1
    Also, "getting your reputation" by answering those low quality questions is precisely why people keep coming back and asking them. Please don't. – Lightness Races in Orbit May 13 '14 at 17:03

0 Answers0