Search code examples
c++language-lawyerpostfix-operator

Class member convertion function-id


I'm looking for a quote from the Stadard described the following behavior:

The following rule is for conversion-type-id lookup (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.

Consider the following example:

#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
}

I don't understand that. The quote above doesn't describe that behavior. It describes the lookup for the conversion-type-id only, but it doesn't describe the conversion-function-id itself.

What is the lookup rule for conversion-function-id?


Solution

  • If a conversion-function-id is explicitly used, as in the example's expression c->operator Z(), the lookup rules are the same as for any other class member. The one trick there is the definition from section 3 paragraph 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.

    The third option here is why the name operator Z used in the expression finds the class member C::operator Y(). It is a member in the accessed class with the same name as the expression's conversion-function-id.