struct X {
void * a;
void * b;
};
X foo( void * u, void * v);
- foo() is implemented in assembler (i386)
address of return value of type X is passed as hidden parameter to foo()
if test code is compiled with -O0 the code works as expected
- if compiled with -O3 segmentation fault happens (return value was optimized out)
- if compiled with -O3 -fno-elide-constructors the code works as expected again
how can the compiler be forced no to add RVO for foo() only (aka not forcing -fno-elide-constructors)?
Update1: the code must work for arbitrary compilers (at least gcc ,clang, msvc), example code:
void * vp = bar();
X x = foo( vp, 0);
x = foo( x.a, 0);
x = foo( x.a, 0);
Update2: the problem is, that the compiler optimizes out the instances of x
X x = foo( vp, 0);
x = foo( x.a, 0);
x = foo( x.a, 0)
or
X x1 = foo( vp, 0);
X x2 = foo( x1.a, 0);
X x3 = foo( x2.a, 0)
doesn't matter. for instance the segfault happens because
X x2 = foo( x1.a, 0);
x1 was optimized out and the implementation tries to access the first argument , which is a null pointer.