gpt4 book ai didi

c++ - 实例化点可以延迟到翻译单元结束吗?

转载 作者:可可西里 更新时间:2023-11-01 15:17:44 29 4
gpt4 key购买 nike

考虑以下小代码片段:

#include <iostream> 

template<class T>
int test();

int main()
{
std::cout << test<int>() << "\n";
}

// POI for test<int>() should be right here

template<class T>
int test()
{
return 0;
}

Live Example为 Clang 和 g++ 编译并打印 0。

这是 draft Standard引用函数模板的实例化点

14.6.4.1 实例化点 [temp.point]

1 For a function template specialization, a member function template specialization, or a specialization for amember function or static data member of a class template, if the specialization is implicitly instantiatedbecause it is referenced from within another template specialization and the context from which it is referenceddepends on a template parameter, the point of instantiation of the specialization is the point ofinstantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specializationimmediately follows the namespace scope declaration or definition that refers to the specialization.

Vandevoorde and Josuttis对此有以下说法:

In practice, most compilers delay the actual instantiation ofnoninline function templates to the end of the translation unit. Thiseffectively moves the POIs of the corresponding templatespecializations to the end of the translation unit. The intention ofthe C++ language designers was for this to be a valid implementationtechnique, but the standard does not make this clear.

问题:Clang/g++ 是否不符合规范,因为它们将 POI 延迟到翻译单元的末尾?

最佳答案

Core Working Group defect report 993旨在解决此问题:

993. Freedom to perform instantiation at the end of the translation unit

Section: 14.6.4.1 [temp.point] Status: C++11 Submitter: John Spicer Date: 6 March, 2009
[Voted into the WP at the March, 2011 meeting.]

The intent is that it is a permissible implementation technique to do template instantiation at the end of a translation unit rather than at an actual point of instantiation. This idea is not reflected in the current rules, however.

Proposed resolution (January, 2011):

Change 14.6.4.1 [temp.point] paragraph 7 as follows:

A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. A specialization for a class template...

C++11 中的段落 14.6.4.1/7 在 N3936 中是 14.6.4.1/8:

A specialization for a function template, a member function template, or of a member function or staticdata member of a class template may have multiple points of instantiations within a translation unit, andin addition to the points of instantiation described above, for any such specialization that has a pointof instantiation within the translation unit, the end of the translation unit is also considered a point ofinstantiation. A specialization for a class template has at most one point of instantiation within a translationunit. A specialization for any template may have points of instantiation in multiple translation units. Iftwo different points of instantiation give a template specialization different meanings according to the onedefinition rule (3.2), the program is ill-formed, no diagnostic required.

所以是的,允许实现将模板的实例化点延迟到翻译单元的末尾。

关于c++ - 实例化点可以延迟到翻译单元结束吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030403/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com