Topic: ctor argument sequence points
Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 4 May 1993 21:08:48 GMT Raw View
Code examples in the most recent issue of c++ report assumes sequence points that I cannot find in the ARM. Has the ANSI c++ committee added a requirement that the argument(s) of a ctor be evaluated IN FULL IMMEDIATELY before the ctor is used.
e.g. Is the output of the following program strictly defined?
#include <iostream.h>
int i = 0;
struct a
{
int a2;
int a1;
a() : a1(++i), a2(++i) {}
};
int main()
{
a *p=new(a);
cout<<p->a1<<','<<p->a2<<endl;
return 0;
}
The c++ compilers I have tried produce the expected result "2,1" but DOES THE LANGUAGE DEFINITION REQUIRE THIS?
Author: steve@taumet.com (Steve Clamage)
Date: Thu, 6 May 1993 16:47:28 GMT Raw View
b91926@fnclub.fnal.gov (David Sachs) writes:
>Code examples in the most recent issue of c++ report assumes sequence points that I cannot find in the ARM. Has the ANSI c++ committee added a requirement that the argument(s) of a ctor be evaluated IN FULL IMMEDIATELY before the ctor is used.
>e.g. Is the output of the following program strictly defined?
>#include <iostream.h>
>int i = 0;
>struct a
>{
> int a2;
> int a1;
> a() : a1(++i), a2(++i) {}
>};
>int main() { ... }
The C++ language specification so far does not have the Standard C
concept of sequence points. (Perhaps that will be added.)
Your example does not have arguments to the ctor, but rather an
initializer list, which is not the same thing.
Members of a class are initialized in declaration order, independent
of initializer order. In your example, a2 must be initialized before a1.
Therefore, a2 must get the current value of i+1, and then a1 must get
the current value of i+1. If the initializers are defined to have a
sequence point between them, then a2 gets 1 and a1 gets 2. Otherwise,
this result is not guaranteed. (Wording about sequence points in this
case has to be carefully worked out, since the textual ordering of
the initializers is not relevent.)
Regarding arguments to constuctors (or to any other function), the
arguments are evaluated before the body of the function is entered.
The order in which the arguments is evaluated is unspecified in C,
and will probably remain unspecified in C++. In C, there is a
sequence point just before the body of the function is entered.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 6 May 1993 18:36:58 GMT Raw View
In article <1993May6.164728.10550@taumet.com>, steve@taumet.com (Steve Clamage) writes:
|> b91926@fnclub.fnal.gov (David Sachs) writes:
|>
|> >Code examples in the most recent issue of c++ report assumes sequence points that I cannot find in the ARM. Has the ANSI c++ committee added a requirement that the argument(s) of a ctor be evaluated IN FULL IMMEDIATELY before the ctor is used.
|>
|> >e.g. Is the output of the following program strictly defined?
|>
|> >#include <iostream.h>
|> >int i = 0;
|> >struct a
|> >{
|> > int a2;
|> > int a1;
|> > a() : a1(++i), a2(++i) {}
|> >};
|>
|> >int main() { ... }
|>
|> The C++ language specification so far does not have the Standard C
|> concept of sequence points. (Perhaps that will be added.)
|>
|> Your example does not have arguments to the ctor, but rather an
|> initializer list, which is not the same thing.
|>
|> Members of a class are initialized in declaration order, independent
|> of initializer order. In your example, a2 must be initialized before a1.
|> Therefore, a2 must get the current value of i+1, and then a1 must get
|> the current value of i+1. If the initializers are defined to have a
|> sequence point between them, then a2 gets 1 and a1 gets 2. Otherwise,
|> this result is not guaranteed. (Wording about sequence points in this
|> case has to be carefully worked out, since the textual ordering of
|> the initializers is not relevent.)
|>
|> Regarding arguments to constuctors (or to any other function), the
|> arguments are evaluated before the body of the function is entered.
|> The order in which the arguments is evaluated is unspecified in C,
|> and will probably remain unspecified in C++. In C, there is a
|> sequence point just before the body of the function is entered.
|> --
|>
|> Steve Clamage, TauMetric Corp, steve@taumet.com
Yes, I did really mean ctor INITIALIZER arguments. Sorry if I used the wrong terminology. Furthermore, my example was deliberately blatant. The actual usage I was referring to was more subtle - something like:
struct a
{
int a1;
int a2;
a(int x) : a1(x),a2(a1) {} // is a2 initialized to x????
};