GCC inlines a statement -- no matter how hard I try to prevent it. I tried
-fno-inline
-O0
__attribute__ ((noinline))
dummy asm("")
No success! Here the code:
#include<iostream>
using namespace std;
struct A {
A() {cout << "A::A()" <<endl; }
A(const A& a) {cout << "A::A(copy)" <<endl; }
A& operator=(const A& a) {cout << "A::=()" <<endl; return *this;}
};
A __attribute__ ((noinline)) func()
{
cout << "func()" << endl;
A loc;
asm("");
return loc;
}
int main() {
A a = func();
}
The unfortunate output of this (g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2) is
func()
A::A()
What happened to the statement A a = func(); ??
The reason for this experiment is that I would like to know what happens when execution comes to this statement (because I need control how this is done):
A a = func();
I read that the copy constructor is called when one does
A a = b;
(In this case the copy-constructor is called. But not in the case A a = func();) The function is inlined instead. I NEED control over this statement since my "struct A" in real life contains dynamically allocated data that needs to be taken care of.
Am I missing something obvious here ?!
-fno-elide-constructors
. (And note that you did declare your constructors inline by virtue of defining them inside the class definition.) – Josiejosler-fno-inline
affects). An inline member function has to do with the ODR, not machine code generation. – Whoso-fno-inline
to me. – Falsework