H
Haochen Xie
Hi everyone,
I just made a test on how C++ would behave when trying to use an object maker function to initialize a variable and my test code gave me the followingresults:
Results:
===============
Before makeBar_val is called
makeBar_val called
Bar 28feff is constructed
After makeBar_val is called, got 28feff
Bar 28feff is destructed
===============
the code is pasted with this post, and also available on pastebin.com: <http://pastebin.com/iKEFv5r5> (pastid: iKEFv5r5).
The code was compiled with mingw32(gcc version 4.6.1 (GCC)) without any optimization option.
The result is what I expected, so the constructor of Bar is only called foronce and the scope of the Bar made by the make function is passed to main(), like the variable bar in main() is directly initialized in place. So here comes my question: is this kind of "inlining" required by the C++ standard or just a smart compiler optimization? Can I assume all compiler would generate a program working like this example? Can I assume the result would be the same if I'm using a non-trivial maker function?
Code:
===============
#include <cstdio>
class Bar
{
public:
// Conster & Dester
Bar() { printf("Bar %x is constructed\n", this); }
~Bar() { printf("Bar %x is destructed\n", this); }
//Copier
Bar(const Bar& bar)
{
printf("Bar is copied (copy constructor is called): "
"%x -> %x\n", &bar, this);
}
const Bar& operator=(const Bar& rhs)
{
printf("Bar is assigned (operator= is called)\n");
return rhs;
}
};
Bar makeBar_val()
{
printf("makeBar_val called\n");
return Bar();
}
int main()
{
printf("Before makeBar_val is called\n");
Bar bar(makeBar_val());
printf("After makeBar_val is called, got %x\n", &bar);
}
===============
I just made a test on how C++ would behave when trying to use an object maker function to initialize a variable and my test code gave me the followingresults:
Results:
===============
Before makeBar_val is called
makeBar_val called
Bar 28feff is constructed
After makeBar_val is called, got 28feff
Bar 28feff is destructed
===============
the code is pasted with this post, and also available on pastebin.com: <http://pastebin.com/iKEFv5r5> (pastid: iKEFv5r5).
The code was compiled with mingw32(gcc version 4.6.1 (GCC)) without any optimization option.
The result is what I expected, so the constructor of Bar is only called foronce and the scope of the Bar made by the make function is passed to main(), like the variable bar in main() is directly initialized in place. So here comes my question: is this kind of "inlining" required by the C++ standard or just a smart compiler optimization? Can I assume all compiler would generate a program working like this example? Can I assume the result would be the same if I'm using a non-trivial maker function?
Code:
===============
#include <cstdio>
class Bar
{
public:
// Conster & Dester
Bar() { printf("Bar %x is constructed\n", this); }
~Bar() { printf("Bar %x is destructed\n", this); }
//Copier
Bar(const Bar& bar)
{
printf("Bar is copied (copy constructor is called): "
"%x -> %x\n", &bar, this);
}
const Bar& operator=(const Bar& rhs)
{
printf("Bar is assigned (operator= is called)\n");
return rhs;
}
};
Bar makeBar_val()
{
printf("makeBar_val called\n");
return Bar();
}
int main()
{
printf("Before makeBar_val is called\n");
Bar bar(makeBar_val());
printf("After makeBar_val is called, got %x\n", &bar);
}
===============