C ++模板的两阶段名称查找 - 为什么? [英] Two phase name lookup for C++ templates - Why?

查看:613
本文介绍了C ++模板的两阶段名称查找 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++标准定义模板的两个相位查找?不能将非依赖声明和定义的查找推迟到实例化阶段?

Why does the C++ standard define two phase lookup for templates? Couldn't non dependent declarations and definitions' lookups be deferred to the instantiation stage as well?

推荐答案

这是模板
最早实现的方式,并且仍然是Microsoft编译器工作的方式。它感到
(在委员会中),这是太容易出错;它使得太容易到
意外劫持一个名称,实例化在一个翻译
单位拾起本地名称,而不是所需的全局符号。 (A
典型的翻译单元将由 #include s,
序列组成,声明每个人都应该看到的名称,然后是
代码。在实例化的时候,
实例化之前的所有内容都是可见的,包括实现代码。)

They could. This is the way most early implementations of templates worked, and is still the way the Microsoft compiler worked. It was felt (in the committee) that this was too error prone; it made it too easy to accidentally hijack a name, with the instantiation in one translation unit picking up a local name, rather than the desired global symbol. (A typical translation unit will consist of a sequence of #includes, declaring the names that everyone should see, followed by implementation code. At the point of instantiation, everything preceding the point of instantation is visible, including implementation code.)

最后的决定是对符号在模板中分成两个
类别:依赖和非依赖,并坚持在
模板的定义点解析
非依赖符号,以降低风险它们偶然地绑定到一些
局部实现符号。加上需要指定
typename 模板,当适用于依赖符号时,
还允许在模板的定义
处进行解析和一些错误检查,而不仅仅是在模板被实例化时。

The final decision was to classify the symbols in a template into two categories: dependent and non-dependent, and to insist that the non-dependent symbols be resolved at the point of definition of the template, to reduce the risk of their accidentally being bound to some local implementation symbols. Coupled with the requirement to specify typename and template when appropriate for dependent symbols, this also allows parsing and some error checking at the point of definition of the template, rather than only when the template is instantiated.

这篇关于C ++模板的两阶段名称查找 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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