Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top