是否应该延迟类模板定义中由 this-> 限定的类/命名空间名称的名称查找?

Should name lookup be deferred for a class/namespace-name qualified by this-> in a class template definition?

本文关键字:gt 命名空间 查找 延迟 定义 this- 是否      更新时间:2023-10-16

clang 3.0和g++4.8.1都拒绝了以下代码,并在注释中显示错误:

template<typename T>
struct S
{
    void f()
    {
        this->dependent(); // no error: type of 'this' is dependent?
        this->Dependent::dependent(); // error: 'Dependent' has not been declared
    }
};

根据【basic.lookup.classref】

后面的类名或命名空间名称。or->运算符在整个后缀表达式的上下文和对象表达式的类的范围中都会查找。

和〔temp.dep.expr〕

如果封闭成员函数的类类型是依赖的,则this是依赖于类型的。

如果类或命名空间名称Dependent是在对象表达式*this的类的作用域中查找的,并且对象表达式的类是依赖的,那么是否应该将此查找推迟到模板实例化之后?标准是否规定了正确的行为?

编辑:clang 3.0接受以下代码,但g++4.8给出了与上述相同的错误

template<typename T>
struct S
{
    T m;
    void f()
    {
        m.dependent();
        m.Dependent::dependent();
    }
};

在您的第一个代码中,两行都是"illformed,no diagnostic required",因为"this"指的是当前实例化,但没有找到成员,类模板也没有依赖基类。

它既不是当前实例化的成员,也不是未知专业化的成员。参见14.6.2.1p6