1

I came to know that Temporaries connot be bound to non-const references.

class X
{
  int i;
};

X fun()
{
 return X();
}
void func(X &x)
{

}

int main()
{

 func(fun());
 return 0;
}

Isn't call to fun producing a temporary? Why can temporary be linked to non-const reference here. I am unable to comprehend as to why is this compiling fine.

EDIT: I am using VS2010. I don't understand how should this matter.

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
  • 2
    Because of an evil MSVC extension. – chris Aug 23 '13 at 17:32
  • How would I know that there was an answer in that duplicate question? – Uchia Itachi Aug 23 '13 at 17:44
  • @UchiaItachi it doesn't matter if you know or not, the point of this site is not to get you rep it's to help you and other people, if someone marks your question as a dupe _that has the answer you need_ you consider the site as having done its job – aaronman Aug 23 '13 at 17:46
  • @UchiaItachi, Sometimes you can't really know or find it, but now there is an additional way to arrive at the answer: through your question. – chris Aug 23 '13 at 17:47

1 Answers1

2

Isn't call to fun producing a temporary?

Yes.

Why can temporary be linked to non-const reference here.

It can't.

I am unable to comprehend as to why is this compiling fine.

Because your compiler is faulty.

I am using VS2010. I don't understand how should this matter.

That compiler has many non-standard "extensions" to the language. This is just one example of dodgy code that's accepted by that compiler, but not a conformant one.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    Ah, It was driving me nuts! Thanks. Maybe I will change the question title with `VS2010` and `temporaries` so that it helps others. – Uchia Itachi Aug 23 '13 at 17:43