c++standards

Is this valid C++?


struct SomeStruct
{
  int a; 
  int b;
};

SomeStruct someFn( int init )
{
  SomeStruct ret = { init, init };
  //...
  return ret;
}

void someFn2( SomeStruct* pStruct )
{
  // ..
}

int main( )
{
  someFn2( &someFn(32) );
  return 0;
}

Solution

  • No, it's not valid.

    From 5.2.2/10 [expr.call] "A function call is an lvalue if and only if the result type is a reference.

    From 5.3.1/2 [expr.unary.op] "The operand shall be an lvalue or a qualified-id".

    someFn(32) is, therefore, not an lvalue as SomeStruct is not a reference and you are using it as the operand to & which requires an lvalue.