可以将实例化延迟到翻译单元的结束吗? [英] Can the point-of-instantiation be delayed until the end of the translation unit?

查看:127
本文介绍了可以将实例化延迟到翻译单元的结束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的小代码片段:

  #include< iostream> 

template< class T>
int test();

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

//测试< int>()的POI应该在这里

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



这是 草拟标准 报价功能模板的实例化



14.6.4.1实例化点[temp.point]


1对于函数模板专门化,成员函数模板专门化或
成员函数或类模板的静态数据成员的专门化,如果专用化是隐式实例化
,因为它是从另一个模板引用的专业化和从中引用的上下文
取决于模板参数,专业化的实例化点是封闭专业化的
实例化的点。否则,这种专业化的实例化点
紧跟在引用专业化的命名空间范围声明或定义之后。


Vandevoorde和Josuttis 对此有以下说明:


在实践中,大多数编译器将
非内联函数模板的实例延迟到翻译单元的末尾。这个
有效地将相应模板
的POI移动到翻译单元的末尾。
C ++语言设计者的意图是这是一个有效的实现
技术,但标准并没有使这一点清楚。


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

解决方案

核心工作创建了组缺陷报告993 以解决此问题:


993。在翻译单元末尾执行实例化的自由



部分:14.6.4.1 [temp.point]状态:C ++ 11发布者:John Spicer日期:2009年3月6日
[在2011年3月的会议上投票进入WP。]



目的是允许实施模板实例化在翻译单元的结尾而不是实际的实例点。



提议的决议(2011年1月):



更改14.6.4.1 [temp.point]第7段如下:


功能模板,成员函数模板或类模板的成员函数或静态数据成员可以在翻译单元内具有多个实例化点,并且除了上述的实例化点之外,对于在翻译单元内具有实例化点的任何这样的专门化类模板的专业化...



C ++ 11中的第14.6.4.1/7节在N3936中是14.6.4.1/8:


对于函数模板,成员函数模板或类模板的成员函数或静态
数据成员的专门化可以在翻译单元内具有多个实例化点,并且
除了对于在翻译单元内具有实例化点
的任何这样的专门化,翻译单元的结束也被认为是
实例化的点。类模板的专门化在翻译
单元内至多有一个实例化点。任何模板的专门化可以在多个翻译单元中具有实例化点。如果
两个不同的实例化点给模板专门化根据一个
定义规则(3.2)的不同含义,程序是不成形的,不需要诊断。


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


Consider the following small code fragment:

#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 that compiles and prints 0 for both Clang and g++.

Here's the draft Standard quote on the point of instantiation of function templates

14.6.4.1 Point of instantiation [temp.point]

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

Vandevoorde and Josuttis have the following to say about this:

In practice, most compilers delay the actual instantiation of noninline function templates to the end of the translation unit. This effectively moves the POIs of the corresponding template specializations to the end of the translation unit. The intention of the C++ language designers was for this to be a valid implementation technique, but the standard does not make this clear.

Question: are Clang/g++ non-conforming because they delay the POI to the end of the translation unit?

解决方案

Core Working Group defect report 993 was created to address this issue:

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...

Paragraph 14.6.4.1/7 in C++11 is 14.6.4.1/8 in N3936:

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 has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. If two different points of instantiation give a template specialization different meanings according to the one definition rule (3.2), the program is ill-formed, no diagnostic required.

So yes, it is allowable for implementations to delay the point of instantiation of templates to the end of the translation unit.

这篇关于可以将实例化延迟到翻译单元的结束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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