Search code examples
c++classlanguage-lawyer

Why can class::A pass compile?


class A {
    public:
    A(){}
};
int main() {
    class::A* a = new class::A();
    return 0;
}

The code above passed compile, it seems that class:: makes no sense, and I wonder why class::A is legal in C++?
Is there some reason for that?


Solution

  • The syntax classId is an elaborated type specifier. Only class types are considered when matching Id.

    The syntax ::Id is a qualified-id. It looks for Id only in the global namespace.

    The combination of the two means that we are looking for a class type in the global namespace, named A. This finds A, so your example compiles.

    It would not find int A;, nor would it find namespace foo { class A{}; }, even within a function defined in namespace foo. Examples