Topic: Standard C++ assignment question.


Author: zalman@macromedia.com (Zalman Stern)
Date: 1996/09/24
Raw View

Is the output of the following program well defined by the C++ standard?

[
#include <stdlib.h>
#include <stdio.h>

void testfunc(int &result) {
        result = 42;
}

void main(void) {
        int main_local;

        testfunc(main_local = 33);
        printf("main_local is %d.\n", value);
        exit(0);
}
]

Specifically, is the compiler required to pass a reference to main_local in
the call to testfunc or can it pass a reference to a temporary?

The April '95 draft standard says the following in section 5.17:
[
There are several assignment operators, all of which group right-to-left.
All require a modifiable lvalue as their left operand, and the type of an
assignment expression is that of its left operand. The result of the
assignment operation is the value stored in the left operand after the
assignment has taken place; the result is an lvalue.
]

A few C++ mavens I've asked claim the last clause stating the result is an
lvalue requires the compiler to pass a reference to main_local. I am not
entirely convinced. The above appears to say the assignment operator must
return an lvalue representing value assigned, but not necessarily the
lvalue assigned to. I note that the draft also doesn't say that the lvalue
returned is modifiable, though I'm not sure this matters. (If it does, I
might conclude that using the result of an assignment operator as a
modifiable lvalue results in an ill-formed program.)

In situations where temporaries are produced by conversions in the
assignment, above mentioned C++ mavens seem to accept that the reference
may not be to the lvalue on the left of the assignment. Section 12.2 on
temporaries defines when one will be destroyed if constructed, but gives
the compiler lots and lots of leeway on whether or not to construct a
temporary.

Of the four C++ compilers I have handy, three pass a reference to
main_local (Microsoft Visual C++ and Motorola's mcc for Power Macintosh,
gcc 2.5.8 on a Sun SPARC) and one passes a reference to a temporary
(Metrowerks CodeWarrior 9, which generates a warning explaining that the
behavior may not be as desired).

What other parts of the draft standard should I be looking at? If this is
unclear, how does one go about getting it clarified? (Short of joining the
committee...)

Another question: is the compiler require to issue a disagnostic for an
"ill-formed" program? There seems to be an exception for "non-diagnosable
semantic rules," but I can't figure out what those are.




[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@taumet.Eng.Sun.COM (Steve Clamage)
Date: 1996/09/24
Raw View
In article rfg@engnews1.Eng.Sun.COM, zalman@macromedia.com (Zalman Stern) writes:

>Another question: is the compiler require to issue a disagnostic for an
>"ill-formed" program? There seems to be an exception for "non-diagnosable
>semantic rules," but I can't figure out what those are.

>From 1.3 "Implementation compliance" and 1.4 "Definitions":

A "well-formed program" is one "constructed according to the syntax rules,
diagnosable semantic rules, and the One Definition Rule."

An "ill-formed program" is one which "is not a well-formed program".

"The set of 'diagnosable semantic rules' consists of all semantic rules
in this International Standard except for those rules containing an
explicit notation that 'no diagnostic is required'."

"Every conforming C++ implemenatation shall ... issue at least one
diagnostic message when presented with any ill-formed program that
contains a violation of any diagnosable semantic rule or any syntax rule."

I hope that makes it clear.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]