A: 

Perfectly legal.

The object will exist on the stack during the function call, just like any other local variable as well.

Nils Pipenbrinck
+1  A: 
mmattax
But the fact that both MSVC and g++ issue warnings for (1) suggests that it may be not be 100% legal. Do you have a section of the C++ standard ti cite?
Adam Rosenfield
+2  A: 

Those A objects will only exist until execution reaches the semicolon. So, the calls are safe, but don't try to save the pointer and use it later. Also, the compiler may require bar take a const reference.

James Curran
A: 

It is legal. We use it sometime to provide a default value which we might want to ignore.

int dosomething(error_code& _e = ignore_errorcode()) {
    //do something
}

In the above case it will construct an empty error code object if no error_code is passed to the function.

roo
I'm pretty sure your ignore_errorcode() returns an error_code object with some more lifetime than an object created by a default constructor...
Pieter
A: 

for //2 you need a const reference

for //1 I think it's legal but useless

ugasoft
+5  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.

Matt Price
This answer is outdated by an edit of the OP
A: 

I have yet to verify it on a compiler, but I guess the foo() function should not compile: You are sending the address to a temporary object inside a function waiting for a pointer (instead of a const pointer).

My guess is that:

void foo(const A *) ;

should work instead.

The old:

void foo(A *) ;

enables the user (the code inside foo) to modify the temporary object, which is not authorized IIRC.

This is for the same reason bar is supposed to work when passing with a parameter with const reference, but won't work when passing the parameter with simple reference.

paercebal
+5  A: 

1: Taking the address of a temporary is not allowed. Visual C++ allows it as a language extension (language extensions are on by default).

2: This is perfectly legal.

James Hopkin
+1  A: 

foo is not allowed in fully standard compliant C++, whereas bar is okay. Though chances are, foo will compile with warning, and bar may or may not compile with a warning as well.

A() create a temporary object, which unless bound to a reference (as is the case in bar), or used to initialize a named object, is destroyed at the end of the full expression in which it was created. A temporary created to hold a reference initializer persists until the end of its reference's scope. For the case of bar, that's the function call, so you can use A inside bar perfectly safely. It is forbidden to bound a temporary object (which is a rvalue) to a non-const reference. It is similarly forbidden to take the address of a rvalue (to pass as argument to initialize A for foo).

KTC
Needs some refactoring to make clear the scope enhancing abilities of a referene on a temporary variable.
Martin York
A: 

Short answer is yes.

If the object is received by function as const reference parameter - as you have modified bar(const A&) method, then it's totally legal. The function can operate on the object, but the object will be destructed after the function call (address of temporary can be taken, but shall not be stored and used after the function call - see reason below).

The foo(A*) is legal too because the temporary object is destroyed at the end of fullexpression. However most of the compiler will emit warning about taking address of temporary.

The original version of bar(A&) shall not compile, it's against the standard to initialize a non-const reference from a temporary.

C++ standard chapter 12.2

3 [...] Temporary objects are destroyed as the last step in evaluating the fullexpression (1.9) that (lexically) contains the point where they were created. [...]

4 There are two contexts in which temporaries are destroyed at a different point than the end of the fullexpression. The first context is when an expression appears as an initializer for a declarator defining an object. In that context, the temporary that holds the result of the expression shall persist until the object’s initialization is complete. [...]

5 The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctorinitializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits.

A fullexpression is an expression that is not a subexpression of another expression.

CsTamas