#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 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有以下说法:
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.
问题: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…
C11中的14.6.4.1/7段在N3936中为14.6.4.1/8:
A specialization for a 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.所以是的,实现允许将模板的实例化点延迟到翻译单元的末尾.
以上是内存溢出为你收集整理的c – 可以将实例点延迟到翻译单元的结尾吗?全部内容,希望文章能够帮你解决c – 可以将实例点延迟到翻译单元的结尾吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)