Topic: qualified identifiers in mem-initializers


Author: Michael Norrish <michael.norrish@nicta.com.au>
Date: Thu, 31 May 2007 09:57:57 CST
Raw View
g++ 4.0.3 rejects the following:

struct D {
   D() : D::y(4) { }
   int y;
};

saying it expects a class-name before the parentheses.

However, 12.6.2 doesn't seem to forbid using qualified names to refer
to fields that are to be initialised.

Where does the standard justify g++'s behaviour?

Thanks,
Michael.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Greg Herlihy <greghe@pacbell.net>
Date: Fri, 1 Jun 2007 09:53:44 CST
Raw View
On May 31, 8:57 am, Michael Norrish <michael.norr...@nicta.com.au>
wrote:
> g++ 4.0.3 rejects the following:
>
> struct D {
>    D() : D::y(4) { }
>    int y;
>
> };
>
> saying it expects a class-name before the parentheses.
>
> However, 12.6.2 doesn't seem to forbid using qualified names to refer
> to fields that are to be initialised.
>
> Where does the standard justify g++'s behaviour?

The member initialization syntax notation in    12.6 describes member
initialization syntax within a constructor initialization list. (Note
that not all forms allowed by the provided syntax are necessarily
valid - the language of the Standard may add additional requirements -
therefore one should think of the syntax notation as describing a
superset of the C++ grammar).

   12.6 defines the syntax for member initialization as follows:

    ctor-initializer:
        ':' mem-initializer-list

    mem-initializer-list:
        mem-initializer
        mem-initializer ',' mem-initializer-list

    mem-initializer:
        mem-initializer-id '(' expression-list_opt ')'

    mem-initializer-id:
        '::'opt nested-name-specifier_opt class-name
        identifier

So the production for a "mem-initializer-id" indicates that - although
a (base) class may be qualified with a nested name specifier or a
double colon - a member name must consist solely of an unqualified
identifier. So in this case, the syntax alone answers the question
because there are no circumstances in C++ in which a qualified member
name in a constructor initializer list is ever allowed.

Greg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]