c++ - 模板库中的名称查找 : why do we add this->

标签 c++ templates language-lawyer argument-dependent-lookup name-lookup

考虑以下:

struct Base {
    void foo();
};

template <class T>
struct Derived : Base {
    void bar() {
        this->foo();
    }
};

通常,我们解释说 this->使 foo()一个依赖名称,所以它的查找被推迟到第二阶段,即模板实例化点。

但是第二阶段查找只调用ADL,不考虑成员函数,是吗?

我很感激任何指向解释上述代码编译原因的标准段落的指针。

最佳答案

But the second phase lookup invokes only ADL, which does not consider member functions, does it?



它不是。然而,ADL 添加到查找集,它并不包含所有查找集。此外,您在此处解释的想法适用于 postfix-expression(args) 中的后缀表达式。 ,当所述后缀表达式是 不合格的 ID .

[temp.dep]

1 ... In an expression of the form:

postfix-expression ( expression-listopt )

where the postfix-expression is an unqualified-id, the unqualified-id denotes a dependent name if

  • [... specific conditions ...]

If an operand of an operator is a type-dependent expression, the operator also denotes a dependent name. Such names are unbound and are looked up at the point of the template instantiation ([temp.point]) in both the context of the template definition and the context of the point of instantiation.



所以如果你在那里 foo()相反,lookup 不会考虑成员,而是只在定义和实例化时尝试自由函数(假设我们有一个依赖表达式,ADL 可以添加到查找集)。

但是对于 this->foo (我故意省略了调用,以讨论后缀表达式),我们有类成员访问权限。此处其他段落适用:

[temp.dep.type]

5 A name is a member of the current instantiation if it is

  • [...]
  • An id-expression denoting the member in a class member access expression for which the type of the object expression is the current instantiation, and the id-expression, when looked up, refers to at least one member of a class that is the current instantiation or a non-dependent base class thereof. [ Note: If no such member is found, and the current instantiation has any dependent base classes, then the id-expression is a member of an unknown specialization; see below.  — end note ]

6 A name is a member of an unknown specialization if it is

  • [...]
  • An id-expression denoting the member in a class member access expression in which either
    • the type of the object expression is the current instantiation, the current instantiation has at least one dependent base class, and name lookup of the id-expression does not find a member of a class that is the current instantiation or a non-dependent base class thereof; or

7 Similarly, if the id-expression in a class member access expression for which the type of the object expression is the current instantiation does not refer to a member of the current instantiation or a member of an unknown specialization, the program is ill-formed even if the template containing the member access expression is not instantiated; no diagnostic required.



这些项目符号告诉我们在 this->foo 时执行什么查找遇到了。它只查找成员。在我们的例子中,我们在当前实例化中有一个非依赖的基类,所以这就是可以明确地找到成员的地方。

找到成员函数,后缀表达式 this->foo表示可调用,这就是函数调用的解析方式。

关于c++ - 模板库中的名称查找 : why do we add this->,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796461/

相关文章:

c++ - Vim 如何添加接受输入的键绑定(bind)

c++ - 以逗号分隔的表达式调用析构函数

c++ - 重载解析和部分模板排序

C++逐字读取文件,不带任何符号

c++ - GCC 4.4 fails to link valid C++11 code, when options are -std=C++0x -O0

Java - 带有泛型参数的泛型类参数

c++ - 隐藏C++模板类成员函数的定义

c++ - 如何正确声明模板类的嵌套类的友元?

c++ - 为什么 bool(val) 比 val.operator bool() 更喜欢双重隐式转换?

c++ - MicroBlaze 上 C++ 的线程安全