c++ - 类成员转换函数-id

标签 c++ language-lawyer postfix-operator

我正在寻找 Stadard 中描述以下行为的引述:

以下规则用于 conversion-type-id 查找 (3.4.6/7):

If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression.

考虑以下示例:

#include <iostream>

class J{ public: static const char a = 'j'; };

typedef J Y;

class C
{
public:
    operator Y(){ std::cout << Y::a; }
};

int main()
{ 
    typedef Y Z;
    C *c= new C(); 
    c -> operator Z(); //C::operator Y is invoked
}

我不明白。上面的引述并没有描述这种行为。它仅描述了对 conversion-type-id 的查找,但未描述 conversion-function-id 本身。

conversion-function-id 的查找规则是什么?

最佳答案

如果显式使用conversion-function-id,如示例表达式c->operator Z(),查找规则与任何其他类(class)成员。一个技巧是第 3 节第 8 段的定义:

Two names are the same if

  • they are identifiers composed of the same character sequence, or

  • they are operator-function-ids formed with the same operator, or

  • they are conversion-function-ids formed with the same type, or

  • they are template-ids that refer to the same class or function, or

  • they are the names of literal operators formed with the same literal suffix identifier.

此处的第三个选项是表达式中使用的名称 operator Z 找到类成员 C::operator Y() 的原因。它是访问类中的一个成员,与表达式的 conversion-function-id 同名。

关于c++ - 类成员转换函数-id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107629/

相关文章:

c++ - 在 std 命名空间中重载(非特化)模板

Scala 中缀/后缀运算符

c - parseInt 的后缀导致错误

c++ - QGraphicsScene - 添加带有少量不透明点的大透明像素图时,应用程序挂起很长时间

c++ - 为什么必须在 C 中向后读取指针声明?

c++ - 在 C++14 的 [class.copy]/12 中加粗文本的目的是什么?

go - 将前缀转换为帖子

c++ - 如何从 C++/MFC 程序中获取控制台窗口?

c++ - 主函数跳过线程并且不加入以显示所需的输出

c++ - 具有多个模板继承的模板实例化