What does the highlighted proposal specification in paragraph 3.3.7 / 1, paragraph 5 mean?

§3.3.7 / 1 clause 5:

The potential scope of a declaration that continues to the end or ends of a class definition also extends to the regions defined by its member definitions, even if members are defined lexically outside the class (this includes static data definitions, nested class definitions, and member function definitions, including member function body, and any part of the declarative part of such a definition following the declarator identifier, including clause-declaration and any default arguments (8.3.6)).

Is it possible to identify such a declaration in the first example in this paragraph?

typedef int c;
enum { i = 1 };
class X {
    char v[i];
    int f() { return sizeof(c); }
    char c;
    enum { i = 2 };
};

      

+3


source to share


2 answers


Yes. The declaration of a c

class member is X

visible within the definition f

, although lexically, it appears later. This means the expression sizeof

is applied to the element and not the type outside, which means it will return 1 and not the size int

(possibly 4).

Also, the enumeration constant X::i

should, according to this rule, be visible when the array is declared v

, although this surprises me and I highly recommend avoiding such code - it sounds like a compiler error or a developer misunderstanding just waiting to happen.



Edit: Lightning strikes in orbit are probably correct that the comment about parts of the declarator only applies to off-line definitions.

+1


source


It looks like it says, among other things, and in addition to the above answer, that given all code outside of this class definition, even if X::f

defined outside of the class, for example:

typedef int c;
enum { i = 1 };
class X {
    char v[i];
    int f();
    char c;
    enum { i = 2 };
};

int X::f() {
    return sizeof(c);
}

      



which, in the context of the definition X::f

, c

will refer to the member variable X::c

and not the typedef

above, because while it looks like it is being defined globally, it f

does live in scope X

.

+1


source







All Articles