Specifically, is the following legal C++?
class A{};
void foo(A*);
void bar(const A&);
int main(void)
{
foo(&A()); // 1
bar(A()); // 2
}
It a
No, it's against the standard to pass a non-const reference to a temporary object. You can use a const reference:
class A{};
void bar(const A&);
int main(void)
{
bar(A()); // 2
}
So while some compliers will accept it, and it would work as long as don't use the memory after the semicolon, a conforming compiler will not accept it.