为什么这样做?
我看到类似的SO问题说它确实如此,但有人可以更详细地解释它吗?特别是,这种行为是否受到标准的保护?
IH
#ifndef I_H_
#define I_H_
typedef void (*FuncPtr)();
template<typename T>
void FuncTemplate() {}
class C {};
#endif
Run Code Online (Sandbox Code Playgroud)
a.cc
#include "i.h"
FuncPtr a() {
return &FuncTemplate<C>;
}
Run Code Online (Sandbox Code Playgroud)
b.cc
#include "i.h"
FuncPtr b() {
return &FuncTemplate<C>;
}
Run Code Online (Sandbox Code Playgroud)
m.cc
#include <iostream>
#include "i.h"
FuncPtr a();
FuncPtr b();
int main() {
std::cout << (a() == b() ? "equal" : "not equal") << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后
$ g++ -c -o a.o a.cc
$ g++ -c -o b.o b.cc
$ g++ -c …Run Code Online (Sandbox Code Playgroud)