gpt4 book ai didi

C++ 查找从属名称

转载 作者:太空狗 更新时间:2023-10-29 23:10:45 25 4
gpt4 key购买 nike

在当前的 C++ 标准(2019 年 3 月)中,有这两段(强调我的):

[temp.res] p.2 :

A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename. [...]

[temp.res] p.9 :

When looking for the declaration of a name used in a template definition, the usual lookup rules ([basic.lookup.unqual], [basic.lookup.argdep]) are used for non-dependent names. The lookup of names dependent on the template parameters is postponed until the actual template argument is known ([temp.dep]). [...]

第一段暗示依赖于模板参数的名称将通过“适用的名称查找”进行查找。

但是,第二段指出依赖于模板参数的名称查找被推迟,直到模板参数已知。

第一段中的示例包括类似于以下情况的情况:

template <typename T>
class A
{
void f()
{
T* p;
}
};

在方法 f 中,我相信 T 是一个“依赖于模板参数的名称”,因此它的查找应该推迟到 T 是已知,因此不存在“适用名称查找”,应假定该名称不是命名类型。

如果第二段引用的段落似乎声明没有执行查找,那么第一段引用的“除非适用的名称查找找到类型名称”是什么意思?我错过了什么吗?

谢谢。

最佳答案

延迟的查找是针对 dependent names 的:例如,foo在类似 T::foo 的上下文中或 Bar<T>::foo , 或 operator@在类似 T() @ 0 的上下文中.显然TBar必须自己查找以了解涉及模板参数(模板参数 can be hidden )并解析模板参数列表。由于(类型)模板参数是 typedef-nameBar 的任何特化是一种类型,即“名称查找找到类型名称”。

关于C++ 查找从属名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55265827/

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