Is it possible to do so? Will the pointer be beaten?

void foo() { SomeType *pObj = nullptr; pObj = &(SomeType(arg1, arg2)); ... // *pObj ? } 
  • one
    I think it is impossible. As I recall, only links prolong the life of a temporary object. Maybe someone will quote from the standard about this. - VladD
  • Will not compile. Please try to compile the code the next time before asking a question. - Abyx

1 answer 1

It is impossible. Only the constant lvalue-link and the rvalue-link prolong the lifespan. Here it is possible:

 void foo() { const SomeType& obj = SomeType(arg1, arg2); ... // obj ! } 

and so:

 void foo() { SomeType&& obj = SomeType(arg1, arg2); ... // obj ! } 

Proof of the above can be found in the C ++ standard (12.2 / 5):

There are two contexts in which you can destroy the ...

The second context is when a reference is bound to a temporary. There are no limits to this subject.

  • Link to the standard find? - VladD
  • @VladD 12.2.4 Draft C ++ 11 - free_ze
  • @VladD, 12.2 / 5 standard - ixSci
  • @ixSci: Oh, cool, plus! - VladD