5

This similar ill-fated question got comments and short answers, before it was closed, to the effect: Because that's how the language is defined. Here I am asking for the evidence within the C++ Standard that it is so defined.

gcc 4.8.1 and clang 3.3 alike, with default diagnostic options or stricter, give errors for extra qualification or explicit qualification on code such as:

struct x
{
    int x::i; // Error: gcc/clang: "extra"
};

int ::y; // Error: gcc: "explicit", clang: "extra"

gcc has diagnosed such errors since v4.1. But popular compilers are not unanimous about these errors. MSVC++ 2012 (Nov CTP) gives an error at int ::y; but even with /Wall, gives no diagnostic at all int x::i; - the kind of case that the ill-fated questioner was raising - and that difference suggests deliberation by the MS compiler writers.

How are these errors warranted by the Standard, if they are? References to the C++11 Standard will suffice.

An answer might be "They follow from grammar". In that case, please try to show how they follow from the grammar and feel free to use the Standard's grammatical classifications. I have a copy and will re-read it to understand the explanation.

Community
  • 1
  • 1
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • 2
    I believe [this](http://stackoverflow.com/questions/17958497/c11-gcc-explicit-qualification-in-declaration-standard-ref/17958535#17958535) is a dupe. It's illegal (per quote in my answer in the dupe) but it'll be allowed in C++1y (see link in litb's answer). – jrok Aug 03 '13 at 09:27
  • I agree that I dupe the linked Q, and it is very helpful. – Mike Kinghan Aug 03 '13 at 11:34
  • 1
    @jrok this is a different question. What is shown in this question stays invalid in C++14. – Johannes Schaub - litb Aug 06 '13 at 20:49

2 Answers2

5

A qualified name in C++ always must refer to a previously declared name. This is specified in clause 8.3 and 3.4.3.2.

You cannot firstly declare a variable or member by using a qualified name - it will end up in a "cannot resolve identifier"-liky compiler error. Such qualifiers are designed to be used for redeclaration. Hence the requirement that these names must find previously declared entities.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
0

It is a bug in the Microsoft compiler to allow x::i within the definition of struct x. There are several of these errors in the MSVC front end, and have been reported to Microsoft but they get closed without being fixed (see similar but different error reported here: https://connect.microsoft.com/VisualStudio/feedback/details/783433/c-compiler-accepts-explicit-constructor-call#details and https://connect.microsoft.com/VisualStudio/feedback/details/794504/keyword-struct-before-constructor-name).

The reason it is invalid is because you are both trying to declare a variable int i and provide a scope using x::i. The scope of the variable is dictated by where it is declared, so trying to declare something with a scope specification is trying to declare it somewhere else, which is invalid.

cdmh
  • 3,294
  • 2
  • 26
  • 41
  • I was "asking for the evidence within the C++ Standard". The Q. linked by jrok provides it. And though scope-resolution in the examples is redundant and in fact C++11-illegal, your own rationale is not right. The scope of a variable is dictated by where it is declared, but the scope resolution operator just attempts to specify the scope in which the operand to its left *is declared*, which for `x::i` in scope `x` or `::y` in global namespace is not "somewhere else", but *is* the scope of the putative declarations. – Mike Kinghan Aug 03 '13 at 13:44
  • I was trying to explain that you can't mix a scope resolution with a definition because they are incompatible. – cdmh Aug 03 '13 at 14:05
  • They are only incompatible when the Standard says so. For most of the time that I've been writing C++ such definitions were legal and commonplace; then they became illegal, and with C++14 it appears they will become legal again. http://www.fourmilab.ch/fourmilog/archives/2006-05/000699.html – Mike Kinghan Aug 03 '13 at 22:42
  • Yes, and doesn't @jrok's reference http://stackoverflow.com/questions/17958497/c11-gcc-explicit-qualification-in-declaration-standard-ref/17958535#17958535 show the Standard forbids `int x::i;` inside `class x`? You quote a 7 year old rant that doesn't describe how this was-wasn't-won't be valid, it just complains about the gcc implementation many years ago. Did I miss something? – cdmh Aug 03 '13 at 22:52
  • You missed the fact that I asked (twice) for evidence from the Standard and instead gave a fallacious *a priori* rationale of your own. You did not answer the question. The linked old rant is randomly selected evidence that code which violates that *a priori* rationale was somehow written and compiled all the time until banned *by the Standard*. That's all I have to say I'm afraid. If you still believe your answer is not poor, make some arbitrary edit to it and that will allow me remove my down-vote. – Mike Kinghan Aug 04 '13 at 07:27