- 论坛徽章:
- 17
|
Inside The C++ Object Model这本书在1996年就写成了,而那时还没有正式的C++规范出现。模板这块内容在标准化过程中变化是非常大的。参与“C++ Templates"这本书的9.5章后记中有如下记载:“非依赖型名称和9.4.2小节讨论的依赖型基类“的名称查找规则是C++标准委员会在1993年引入的。Bjarne Stroustrup在1994年初出版的[StroustrupDnE]首次给出了这些内容。而惠普wcngd1997年初才把它引入C++编译器。”,根据以上内容推断Inside The C++ Object Model关于你说的这个问题的描述应该是不准确的。书中至少没有准确描述涉及name lookup时的"Two Phase Lookup"问题。
"Two Phase Lookup"在第1阶段,当使用普通查找规则对模板进行解析时,就会查找非依赖型名称。另外,非受限的依赖型名称(诸如函数调用中的函数名称,之所以说它是依赖型,是因为该名称具有一个依赖型实参)也会在这个阶段进行查找。第2阶段发生在模板被实例化的时候,非受限的依赖型名称在此阶段也会再次执行ADL查找(基本类型int,double等没有关联的名字空间,因此不会发生ADL查找)。
楼主的例子的情况是典型的“依赖型非受限名称”问题,根据之前的描述以及C++标准中相关说明g++中的行为才是符合标准的
而C++2003标准对部分的规定有如下内容:
14.6.4.2 Candidate functions
For a function call that depends on a template parameter, if the function name is an unqualified-id but not a template-id, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:
— For the part of the lookup using unqualified name lookup (3.4.1), only function declarations with external linkage from the template definition context are found.
— For the part of the lookup using associated namespaces (3.4.2), only function declarations with external linkage found in either the template definition context or the template instantiation context are found.
|
|