Is this valid C++ code?

后端 未结 4 796
迷失自我
迷失自我 2021-02-04 00:37

I had the following code, which was basically,

class foo {
  public:
    void method();
};

void foo::foo::method() { }

I had accidentally adde

4条回答
  •  野性不改
    2021-02-04 01:18

    If I read the standard correctly, g++ is right and VS is wrong.

    ISO-IEC 14882-2003(E), §9.2 Classes (pag.153): A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

    Following on the comments below, it's also particularly useful to retain the following concerning the actual Name Lookup rules:

    ISO-IEC 14882-2003(E), §3.4-3 Name Lookup (pag.29): The injected-class-name of a class (clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup.

    It would be odd if it wasn't, given the final part of text at 9.2. But as litb commented this reassures us that indeed g++ is making a correct interpretation of the standard. No questions are left.

提交回复
热议问题