Why does this work?
I see similar SO questions stating that it does, but could someone explain it in more detail? Particularly, is this behavior protected by a standard?
i.h
#ifndef I_H_
#define I_H_
typedef void (*FuncPtr)();
template<typename T>
void FuncTemplate() {}
class C {};
#endif
a.cc
#include "i.h"
FuncPtr a() {
return &FuncTemplate<C>;
}
b.cc
#include "i.h"
FuncPtr b() {
return &FuncTemplate<C>;
}
m.cc
#include <iostream>
#include "i.h"
FuncPtr a();
FuncPtr b();
int main() {
std::cout << (a() == b() ? "equal" : "not equal") << std::endl;
return 0;
}
Then
$ g++ -c -o a.o a.cc
$ g++ -c -o b.o b.cc
$ g++ -c -o m.o m.cc
$ g++ a.o b.o m.o -o prog
$ ./prog
equal
Tossing -Wall -Wextra -Werror -ansi
onto all the g++
calls produces the same.
My (naive) understanding is that FuncTemplate
is instantiated once in each of the a.o
and b.o
compilation units, and so the addresses should each point to one copy. How do these end up the same after all, and is this behavior portable or protected?
EDIT The shared library case:
$ g++ -shared -o liba.so a.cc
$ g++ -shared -o libb.so b.cc
$ g++ -c -o m.o m.cc
$ g++ -L. -la -lb m.o -o prog
$ ./prog
equal