标签: c++-address

跨编译单元的相同功能模板实例的地址

为什么这样做?

我看到类似的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)

c++ linker templates function c++-address

7
推荐指数
1
解决办法
1010
查看次数

标签 统计

c++ ×1

c++-address ×1

function ×1

linker ×1

templates ×1