Assume the following source file (translation unit; TU):
struct X {
int i;
X(const X&);
X(X&&);
};
X::X(const X&) = default;
X::X(X&&) = default
If I compile it with Clang, it generates the machine code for both copy and move constructors. However, GCC generates it for the move constructor only, and the machine code of the copy constructor is missing in the resulting object file. I first thought it was some problem related to the Compiler Explorer, but then I observed the same behavior on my local Linux system using objdump.
Live demo: https://godbolt.org/z/1re4brr6P
I don't understand this, because, once I have another TU with a copy constructor call, it needs to be called from the machine code: https://godbolt.org/z/3YaMTd5do. So, GCC generates a call of the copy constructor in the second TU, but does not generate its machine code in the first TU. How can then the linker link the object files together?
gccsimply merges both constructors into one chunk of machine code. The symbol for the copy ctor is there, godbolt just filters it out. Usenm -C myfile.olocally to see it..set foo, barto definefooas having the same address asbar.