I had a discussion with one programmer, and his main task was that the next statement in foo can be passed or not, depending on the compiler.
#include <cassert> const int i = 0 ; void foo ( const int& i ) { assert( &::i == &i ) ; } int main ( ) { foo( i ) ; }
he told me that the expression (& i) can be evaluated to refer to some temporary object. since I have doubts, I'm here. how can the temp reference be passed to the function, if in the function I can check and do whatever I want with i, and the parameter addresses and expected semantics should be saved.? eg
#include <initializer_list> const int i = 0 ; bool func ( const int & i ) { return &::i == & i ; } int main () { const int i = 0 ; for ( const int * each : { &::i , &i , } ) if ( func( * each ) ) break ; // etc }
Probably such a thing could happen somewhere, but not for this case. thatβs how I think, but I canβt get the full proof from the standard.
what i already found (thanks npw ):
from [ expr.call # 5 ]:
If the parameter is a const reference type, then the temporary object is (if necessary) ([dcl.type], [lex.literal], [lex.string], [dcl.array], [class.temporary])
the first four links are not applicable to my case, but the fifth gives hope.
so my question is: does the standard give a guarantee, will this statement in foo be fulfilled.?
source share