Suppose I have a contrived header file containing a single template class and two source files containing the exact same instantiation of the template class, along with one duplicate function. That is...
Contrived header (thing.hpp):
#pragma once
template <typename T>
class Thing {
public:
T t;
public:
T& value() {
return t;
}
};
thing.cpp:
#include <thing.hpp>
template class Thing<int>;
int MeaningOfLife() {
return 42;
}
thingy.cpp: (exactly the same as thing.cpp)
Upon compilation and linkage (using clang on OS X), it seems that only MeaningOfLife
was seen as a duplicate symbol, yet the symbols for the template instantiation (which is just Thing::value()
) were not. Upon closer inspection of the disassembly, it seems that an assembly directive was placed on the symbol for Thing::value()
called .weak_definition
.
Question 0: It's implied that this directive is doing something to prevent the symbol(s) from being multiply defined, but what is it really doing?
Question 1: How might this be done elsewhere (e.g. on Linux, Windows, etc.) by other compilers?
Question 2: What if I perversely altered the assembly code of one of the duplicated template instantiations? That is, same symbols, different function body. Would a smart compiler detect discrepancies?