the following code compiles with Visual Studio 2008 but not with g++ on Mac OSX:
class A
{
public:
A CreateA()
{
A a;
return a;
}
};
class B
{
public:
void ComputeWithA
(
A &a // Here, removing the reference solves the problem
)
{
return;
}
};
int main ()
{
A a;
B b;
b.ComputeWithA(a); // Works
b.ComputeWithA(a.CreateA()); // Invalid arguments 'Candidates are: void ComputeWithA(A &)'
return 0;
}
Why this reference related issue? Any explanation would be greatly appreciated.
Best regards.
a.CreateA()gives you a R-Value (i.e. a temporary).ComputeWithAwants a reference, aka L-Value. The standard says that you can’t convert R- into L-Values, so the MSVC is wrong here.However, you can take a const reference since this case is explicitly allowed: