Error in the C++ Standard?

M

muler

[C++ 98] 3.3.6.5 The potential scope of a declaration that extends to
or past the end of a class definition also extends to the regions
defined by its member definitions, even if the members are defined
lexically outside the class (this includes static data member
definitions, nested class definitions, member function definitions
(including the member function body and, for constructor functions
(12.1), the ctor-initializer (12.6.2)) and any portion of the
declarator part of such definitions which follows the identifier,
including a parameter-declaration-clause and any default arguments
(8.3.6).

// example:
int x = -1;

class Test {
public:
// according to the standard, global x is in scope of ctor-
initializer, so why is this an error?
Test() : x(-5) {}
};

I used visual studio C++ compiler (2010).

I never write such a weird looking code, but I want to understand what
the standard is saying.

Thanks,
 
V

Victor Bazarov

[C++ 98] 3.3.6.5 The potential scope of a declaration that extends to
or past the end of a class definition also extends to the regions
defined by its member definitions, even if the members are defined
lexically outside the class (this includes static data member
definitions, nested class definitions, member function definitions
(including the member function body and, for constructor functions
(12.1), the ctor-initializer (12.6.2)) and any portion of the
declarator part of such definitions which follows the identifier,
including a parameter-declaration-clause and any default arguments
(8.3.6).

// example:
int x = -1;

class Test {
public:
// according to the standard, global x is in scope of ctor-
initializer, so why is this an error?
Test() : x(-5) {}

Could it be because that only base class names and member names can
occur in the initializer list outside of parentheses? If you wanted to
use 'x' to initialize some member, like

Test() : memberX(x) {}

, that should be OK. But 'memberX' needs to be either the name of a
data member or of a base class.
};

I used visual studio C++ compiler (2010).

I never write such a weird looking code, but I want to understand what
the standard is saying.

V
 
B

Balog Pal

muler said:
// example:
int x = -1;

class Test {
public:
// according to the standard, global x is in scope of ctor-
initializer, so why is this an error?
Test() : x(-5) {}
};

x i in scope and works, if you observe all the other rules also, i.e.

class Test {
int m_i;
public:
Test() : m_i(x) {}
};
 
J

Johannes Schaub (litb)

muler said:
[C++ 98] 3.3.6.5 The potential scope of a declaration that extends to
or past the end of a class definition also extends to the regions
defined by its member definitions, even if the members are defined
lexically outside the class (this includes static data member
definitions, nested class definitions, member function definitions
(including the member function body and, for constructor functions
(12.1), the ctor-initializer (12.6.2)) and any portion of the
declarator part of such definitions which follows the identifier,
including a parameter-declaration-clause and any default arguments
(8.3.6).

// example:
int x = -1;

class Test {
public:
// according to the standard, global x is in scope of ctor-
initializer, so why is this an error?
Test() : x(-5) {}
};

I used visual studio C++ compiler (2010).

That paragraph of the Standard has a problem.

- It must restrict the potential scope of the name within the class to the
part of the class specifier that appears after the point of declaration of
the member. As it stands, the text makes this code valid:

struct A {
void f(type) { } // "any portion of the declarator part"
typedef int type; // extends to the end of the class definition
};

However, that code is intended to be invalid. But the code you show is
clearly invalid. "x" is indeed in scope - unqualified name lookup will find
it. But it won't refer to a non-static data member of Test so it won't be
valid. See 12.6.2/2.

This is like asking. `Why is 10 = 2 an error? Both are valid integers.`.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top