Topic: order of initialization


Author: marco@technoboredom.net (Marco Manfredini)
Date: Tue, 12 Jun 2001 19:12:59 GMT
Raw View
Steve Clamage <clamage@eng.sun.com> wrote in
news:Pine.SOL.3.96.1010611084516.9727C-100000@taumet:

> On Mon, 11 Jun 2001, Marco Manfredini wrote:
>> >
>> > The order of declaration (and that also applies when you have
>> > multiple base classes. It is very unwise for maintenance
>> > programmers to play around with the order of declarations unless
>> > they understand the consequences
>> >
>>
>> Wouldn't it be a good idea then to make it mandatory for a
>> compiler to bark if the initialization order does not match the
>> declaration order? Why is it allowed this way? (Actually, I wonder
>> because I fall for this recently..)
>
> The standard does not specify "warnings", or the content of
> error messages, or even on how diagnostic messages are to be
> produced. One reason is that program development evironments
> are too diverse to say anything meaningful. Another is that
> we don't want to limit innovative techniques in compiler
> error detection and recovery.
[snip]

No, with 'mandatory' I just mean to add a paragraph to 12.6.2 saying
that the order of the ctor-initializer list must match the order of the
base and member initialization given through paragraph 5 (which
describes the construction order), for a program to be well-formed.


--
Marco

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: marco@technoboredom.net (Marco Manfredini)
Date: Mon, 11 Jun 2001 11:26:23 GMT
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> wrote in
news:QMkRPCFIZ+H7Ewvi@ntlworld.com:

> In article <9fol0l$52$1@woodrow.ucdavis.edu>, Wei Ku
><weiku@lifshitz.ucdavis.edu> writes
>>Hello, all:
>>
>>Is the order of initialization of member variables determined by
>>the order they appear in the initialization list of the
>>constructor, or by the order they appear in the declaration of the
>>class ?  For, example,
>
> The order of declaration (and that also applies when you have
> multiple base classes. It is very unwise for maintenance
> programmers to play around with the order of declarations unless
> they understand the consequences
>

Wouldn't it be a good idea then to make it mandatory for a compiler to
bark if the initialization order does not match the declaration order?
Why is it allowed this way? (Actually, I wonder because I fall for this
recently..)

--
Marco

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Steve Clamage <clamage@eng.sun.com>
Date: Mon, 11 Jun 2001 18:26:47 GMT
Raw View
On Mon, 11 Jun 2001, Marco Manfredini wrote:
> >
> > The order of declaration (and that also applies when you have
> > multiple base classes. It is very unwise for maintenance
> > programmers to play around with the order of declarations unless
> > they understand the consequences
> >
>
> Wouldn't it be a good idea then to make it mandatory for a compiler to
> bark if the initialization order does not match the declaration order?
> Why is it allowed this way? (Actually, I wonder because I fall for this
> recently..)

The standard does not specify "warnings", or the content of
error messages, or even on how diagnostic messages are to be
produced. One reason is that program development evironments
are too diverse to say anything meaningful. Another is that
we don't want to limit innovative techniques in compiler
error detection and recovery.

The standard just says that if certain kinds of errors are
present in the code, a diagnostic message is required. The
standard can't say that each error requires a message, or
what a message should say, because invalid code could be
interpreted as being wrong in different ways.

The old joke is that a compiler can meet the error diagnosistic
requirements by simply emitting
 "This program might contains errors."
at the end of EVERY compilation.

We leave the details as a "quality of implementation" issue.
In particular, a compiler can emit diagnostic messages of
any sort, as long as it compiles valid code according to the
requirements of the standard.

If you would like to see better or more detailed diagnostic
messages from your compiler, or additional warnings for
questionable code, you should ask your vendor. Many vendors
are responsive to such requests.

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Balog Pal (mh)" <pasa@lib.hu>
Date: Thu, 7 Jun 2001 21:33:51 GMT
Raw View
Wei Ku wrote in message <9fol0l$52$1@woodrow.ucdavis.edu>...

>Is the order of initialization of member variables determined by the order
>they appear in the initialization list of the constructor, or by the order
>they appear in the declaration of the class ?

The latter. The init list order is completely ignored. Some compilers may
warn you if the order is different from that of the class, but better not
rely on that.

Paul


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Wei Ku" <weiku@lifshitz.ucdavis.edu>
Date: Thu, 7 Jun 2001 19:36:23 GMT
Raw View
Hello, all:

Is the order of initialization of member variables determined by the order
they appear in the initialization list of the constructor, or by the order
they appear in the declaration of the class ?  For, example,

class A {
public:
  double b;
  int a;
  A( void) : a( 1), b( a) {}
};

Should variable b be properly initialized to 1 in the above example?  I have
tried g++, KCC, and VC++ 6 and they all failed to do so.  Any comment is
very welcome.

Sincerely,
Wei


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 7 Jun 2001 20:48:33 GMT
Raw View
In article <9fol0l$52$1@woodrow.ucdavis.edu>, Wei Ku
<weiku@lifshitz.ucdavis.edu> writes
>Hello, all:
>
>Is the order of initialization of member variables determined by the order
>they appear in the initialization list of the constructor, or by the order
>they appear in the declaration of the class ?  For, example,

The order of declaration (and that also applies when you have multiple
base classes. It is very unwise for maintenance programmers to play
around with the order of declarations unless they understand the
consequences


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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.research.att.com/~austern/csc/faq.html                ]