跨编译单元的相同功能模板实例化的地址 [英] Addresses of identical function template instantiations across compilation units

查看:151
本文介绍了跨编译单元的相同功能模板实例化的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这项工作?

我看到类似的SO问题说明它,但有人可以更详细地解释它吗?特别是,这种行为是否受到标准的保护?

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?

ih

#ifndef I_H_
#define I_H_

typedef void (*FuncPtr)();

template<typename T>
void FuncTemplate() {}

class C {};

#endif

a.cc / p>

a.cc

#include "i.h"

FuncPtr a() {
  return &FuncTemplate<C>;
}

b.cc

#include "i.h"

FuncPtr b() {
  return &FuncTemplate<C>;
}

m.cc
$ b

m.cc

#include <iostream>

#include "i.h"

FuncPtr a();
FuncPtr b();

int main() {
  std::cout << (a() == b() ? "equal" : "not equal") << std::endl;

  return 0;
}

然后

$ 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

-Wall -Wextra -Werror -ansi 放到所有 g ++ 调用上会产生相同的结果。

Tossing -Wall -Wextra -Werror -ansi onto all the g++ calls produces the same.

我(天真)的理解是 FuncTemplate 在每个 ao bo 编译单元,因此地址应该指向一个副本。

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?

编辑共享库的情况:

$ 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


推荐答案

这是一个定义规则所涵盖的:

This is covered under the one definition rule:


类类型可以有多个定义(第9条) ,枚举类型(7.2),具有外部链接的内联函数(7.1.2),类模板(第14条),非静态函数模板(14.5.6) (14.5.1.3),类模板(14.5.1.1)的成员函数或者在程序中没有指定某些模板参数(14.7,14.5.5)的模板特化,只要每个定义出现在不同的翻译单元中,并且提供的定义满足以下要求。给定这样一个在多个翻译单元中定义的D实体,则

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

有一个完整的标准列表,坚持或其未定义的行为。在上面这些都有。然后...

There is a whole list of criteria that follow that have to be-adhered to or its undefined behavior. In the above these do hold. Then ...


如果D的定义满足所有这些要求,那么程序将表现得像D的单个定义

If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D.

因此,在技术上,您可以在每个翻译单元中拥有该函数的副本。

So technically you can have a copy of the function in each translation unit.

它看起来像最后一个短语中的措辞,但它要求它们都表现相同。这意味着取任何这些对象的地址应该导致相同的地址。

It looks like the wording in the last phrase though makes it a requirement that they all behave the same. This means taking the address of any of these objects should result in the same address.

这篇关于跨编译单元的相同功能模板实例化的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆