Related to this
I want to know what exactly is a nested name specifier? I looked up in the draft but I could understand the grammar as I haven't taken any Compiler Design classes yet.
void S(){}
struct S{
S(){cout << 1;}
void f(){}
static const int x = 0;
};
int main(){
struct S *p = new struct ::S;
p->::S::f();
S::x;
::S(); // Is ::S a nested name specifier?
delete p;
}
::S
is a qualified-id.
In the qualified-id ::S::f
, S::
is a nested-name-specifier.
In informal terms1, a nested-name-specifier is the part of the id that
::
) if one appears at the very beginning of the id andVery informally1, an id is either a qualified-id or an unqualified-id. If the id is a qualified-id, it is actually composed of two parts: a nested-name specifier followed by an unqualified-id.
Given:
struct A {
struct B {
void F();
};
};
A
is an unqualified-id.::A
is a qualified-id but has no nested-name-specifier.A::B
is a qualified-id and A::
is a nested-name-specifier.::A::B
is a qualified-id and A::
is a nested-name-specifier.A::B::F
is a qualified-id and both B::
and A::B::
are nested-name-specifiers.::A::B::F
is a qualified-id and both B::
and A::B::
are nested-name-specifiers.[1] This is quite an inexact description. It's hard to describe a grammar in plain English...
A nested namespace specifier is:
nested-name-specifier :
class-or-namespace-name::nested-name-specifier(optional)
That is, a non-empty list of namespaces and classnames, each followed by ::, representing a relative branching in the overall "namespace tree" of the program. For example, my_namespace::
, my_namespace::inner_namespace::
, my_namespace::my_class::
, and my_class::
.
Note specifically the difference from:
qualified-namespace-specifier :
::(optional) nested-name-specifier(optional) class-or-namespace-name
In that a nested-name-specifier may not be absolute (prefixed with ::
to refer to the global scope), while a qualified-namespace-specifier can be, but doesn't end with ::
.
In your example, ::S
resolves to the function ::S()
, and not the struct (precendence rules for that were discussed here on Stackoverflow in the question you linked to at the start of your question), so it is not a nested name specifier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With