473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

3.3.6 - Class scope: reordering member declarations

If find the following excerpt from the Standard a bit confusing:
<quote>
3.3.6 - Class scope [basic.scope.cla ss]

-1- The following rules describe the scope of names declared in classes.

1) The potential scope of a name declared in a class consists not only of
the declarative region following the name's declarator, but also of all
function bodies, default arguments, and constructor ctor-initializers in
that class (including such things in nested classes).

2) A name N used in a class S shall refer to the same declaration in its
context and when re-evaluated in the completed scope of S. No diagnostic is
required for a violation of this rule.

3) If reordering member declarations in a class yields an alternate valid
program under (1) and (2), the program is ill-formed, no diagnostic is
required.
</quote>

If I change the order of initialization of class member variables, that can
certainly change the behavior of a program. I believe both of the
following are valid class definitions. They will, however, result in
different initial states when constructed with the same actual parameter.

struct S{ int a, b; S(int bb):a(b),b(bb){ } };
struct T{ int b, a; T(int bb):a(b),b(bb){ } };

That seems to contradict 3) above. Aren't two programs that behave
differently "alternate valid programs"? I'm confident that I am failing to
understand something here, but I don't see what it might be. Any ideas?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 6 '06 #1
5 2031

Steven T. Hatton wrote:
If find the following excerpt from the Standard a bit confusing:
<quote>
3.3.6 - Class scope [basic.scope.cla ss]

-1- The following rules describe the scope of names declared in classes.

1) The potential scope of a name declared in a class consists not only of
the declarative region following the name's declarator, but also of all
function bodies, default arguments, and constructor ctor-initializers in
that class (including such things in nested classes).

2) A name N used in a class S shall refer to the same declaration in its
context and when re-evaluated in the completed scope of S. No diagnostic is
required for a violation of this rule.

3) If reordering member declarations in a class yields an alternate valid
program under (1) and (2), the program is ill-formed, no diagnostic is
required.
</quote>

If I change the order of initialization of class member variables, that can
certainly change the behavior of a program. I believe both of the
following are valid class definitions. They will, however, result in
different initial states when constructed with the same actual parameter.

struct S{ int a, b; S(int bb):a(b),b(bb){ } };
struct T{ int b, a; T(int bb):a(b),b(bb){ } };

That seems to contradict 3) above. Aren't two programs that behave
differently "alternate valid programs"? I'm confident that I am failing to
understand something here, but I don't see what it might be. Any ideas?
There is only one class declaration which does not change - so there is
only one program being considered. The "reordering " of the class
declaration is strictly conceptual. The compiler evaluates the class
declaration from two different angles - and if the interpretation of
the declaration changes as a consequence - the program is ill-formed
(though the compiler is not obliged to tell you that).

Greg

Dec 6 '06 #2
* Steven T. Hatton:
If find the following excerpt from the Standard a bit confusing:
<quote>
3.3.6 - Class scope [basic.scope.cla ss]

-1- The following rules describe the scope of names declared in classes.

1) The potential scope of a name declared in a class consists not only of
the declarative region following the name's declarator, but also of all
function bodies, default arguments, and constructor ctor-initializers in
that class (including such things in nested classes).

2) A name N used in a class S shall refer to the same declaration in its
context and when re-evaluated in the completed scope of S. No diagnostic is
required for a violation of this rule.

3) If reordering member declarations in a class yields an alternate valid
program under (1) and (2), the program is ill-formed, no diagnostic is
required.
</quote>

If I change the order of initialization of class member variables, that can
certainly change the behavior of a program. I believe both of the
following are valid class definitions. They will, however, result in
different initial states when constructed with the same actual parameter.

struct S{ int a, b; S(int bb):a(b),b(bb){ } };
struct T{ int b, a; T(int bb):a(b),b(bb){ } };

That seems to contradict 3) above. Aren't two programs that behave
differently "alternate valid programs"? I'm confident that I am failing to
understand something here, but I don't see what it might be. Any ideas?
S exhibits Undefined Behavior.

But I'm not sure what the text you quoted really means.

*Checking out the standard, looking for further info*...

Well, would you look at that, there's an example following para 5.

Now at least para 2 is clear (it refers to multiple declarations of the
same name, where which one is referred to could be changed by reordering
except that para 2 forbids it), but I'm still not sure about 3.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 6 '06 #3

Alf P. Steinbach wrote:
*Checking out the standard, looking for further info*...

Well, would you look at that, there's an example following para 5.

Now at least para 2 is clear (it refers to multiple declarations of the
same name, where which one is referred to could be changed by reordering
except that para 2 forbids it), but I'm still not sure about 3.
There was a thread on comp.c++.modera ted about "reordering " class
declarations. See http://tinyurl.com/yjdq4p and follow-ups posts.

Greg

Dec 6 '06 #4
Alf P. Steinbach wrote:
* Steven T. Hatton:
>If find the following excerpt from the Standard a bit confusing:
<quote>
3.3.6 - Class scope [basic.scope.cla ss]

-1- The following rules describe the scope of names declared in classes.

1) The potential scope of a name declared in a class consists not only of
the declarative region following the name's declarator, but also of all
function bodies, default arguments, and constructor ctor-initializers in
that class (including such things in nested classes).

2) A name N used in a class S shall refer to the same declaration in its
context and when re-evaluated in the completed scope of S. No diagnostic
is required for a violation of this rule.

3) If reordering member declarations in a class yields an alternate valid
program under (1) and (2), the program is ill-formed, no diagnostic is
required.
</quote>

If I change the order of initialization of class member variables, that
can
certainly change the behavior of a program. I believe both of the
following are valid class definitions. They will, however, result in
different initial states when constructed with the same actual parameter.

struct S{ int a, b; S(int bb):a(b),b(bb){ } };
struct T{ int b, a; T(int bb):a(b),b(bb){ } };

That seems to contradict 3) above. Aren't two programs that behave
differently "alternate valid programs"? I'm confident that I am failing
to
understand something here, but I don't see what it might be. Any ideas?

S exhibits Undefined Behavior.

But I'm not sure what the text you quoted really means.

*Checking out the standard, looking for further info*...

Well, would you look at that, there's an example following para 5.

Now at least para 2 is clear (it refers to multiple declarations of the
same name, where which one is referred to could be changed by reordering
except that para 2 forbids it), but I'm still not sure about 3.
I believe I finally figured it out. It hinges on what is meant by "under
(1) and (2)". It just means "we are only talking about reordering as it
involves these rules".

As for S exhibiting undefined behavior, I thought it would be unspecified
behavior. That is, it is legal to use an uninitialized int, there's just
no guarantee as to its value.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 6 '06 #5
On 6 Dec 2006 04:22:11 -0800 in comp.lang.c++, "Greg"
<gr****@pacbell .netwrote,
>There was a thread on comp.c++.modera ted about "reordering " class
declarations . See http://tinyurl.com/yjdq4p and follow-ups posts.
Never believe anything that has to hide behind tinyurl.com.

A legitimate URL for the same article is
http://groups.google.com/gr*********...oglegroups.com
Dec 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2168
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >> (istream &, abc &); private:
30
2252
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
9
8250
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
8
3367
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
37
2582
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
4
3328
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
7
2106
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
10
3508
by: =?Utf-8?B?TWF4IDFlNg==?= | last post by:
I have an application form named Form1.h. The code for this form is a namespace (MyNamespace) with two classes in it (Form1 and MyClass). Form1 has windows designer code and some button event handlers. In those event handlers I want to reference methods in the MyClass class. I use syntax like this: double F = MyClass::MyMethod(x,y); As soon as I type the second colon I get a full list of all the exposed methods in MyClass and I sense I...
15
7854
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.