Why do the following code generate such output?
main.cpp ctor 0x24a4c30
test.cpp dtor 0x24a4c30
test.cpp
#include <boost/optional.hpp>
struct Test
{
Test()
{
printf("test.cpp ctor %p\n", (void *) this);
}
~Test()
{
printf("test.cpp dtor %p\n", (void *) this);
}
};
boost::optional< Test > t;
main.cpp
#include <memory>
struct Test
{
Test()
{
printf("main.cpp ctor %p\n", (void *) this);
}
~Test()
{
printf("main.cpp dtor %p\n", (void *) this);
}
};
int
main(void)
{
std::make_shared< Test >();
return 0;
}
Compiled with
g++ -std=c++11 -c test.cpp -o test.o
g++ -std=c++11 -c main.cpp -o main.o
g++ -std=c++11 test.o main.o
I explain this behaviour that the test.o provide Test's ctor & dtor then a linker discard duplicated symbols from main.o, but it's correct only for dtor. If I delete the static object t then a linker discard symbols from test.o and the output is next
main.cpp ctor 0x208ec30
main.cpp dtor 0x208ec30