Re: Teach myself C++.



In message <87r5w1o1wl.fsf@xxxxxxxxxxxxxxxxxxxx>, Phil Carmody <thefatphil_demunged@xxxxxxxxxxx> writes
Richard Herring <junk@[127.0.0.1]> writes:
In message
<1d1e1eb2-c405-42c2-b9c1-6e9cf0a049c5@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Tom St Denis <tom@xxxxxxx> writes
On Jul 24, 1:21 pm, Jonathan Lee <cho...@xxxxxxx> wrote:
On Jul 24, 8:16 am, Tom St Denis <t...@xxxxxxx> wrote:

> In an embedded development sense you really don't want C++'s automatic
> constructors messing with memory like in some statement like

> a = (b + 3) * (c - d * 4) / a;

I know it's just a toy example, but nevertheless this could be
rewritten

bigint t1 = b + 3;
bigint t2 = d * 4;
bigint t3 = c - t2;
t1 *= t3;
t1 /= a;
a = t1;

There would likely not be any temps (with RVO), it's only a line
longer than your C equivalent, and the notation looks better (to me).

Actually expressions like

bigint a = b + c;

Could easily involve 4 bigints ... that is a, b, c and the expression
(b + c) which is an operand to the = operator. The compiler may
optimize it to alias that to 'a' for all I know. Worse, it might not
delete[] (b + c) until the scope of the statement or variables is lost
(e.g. freed when the function returns).

Only if it's a broken compiler. A compliant one is guaranteed to
invoke the destructor of the temporary at the end of the
full-expression, which in this case is the semicolon.

What if it is a compiler which conforms to this version of the C++ standard:
"""
2 On exit from a scope (however accomplished), destructors are called for
all constructed objects with automatic storage duration (named objects
or temporaries) that are declared in that scope, in the reverse order
of their declaration.
"""
?

That would be both of them. (ISO 14882:1998(E) and ISO/IEC 14882:2003(E).). No doubt it will also be in the forthcoming C++0x standard, but I haven't checked.

Unfortunately, you have taken it out of context. It's part of 6.6 [stmt.jump] and is describing the consequences of a break, continue, goto or return statement. I don't see a break, continue, goto or return in the code above.

If you had read a little further on in the standard, you would have found the section which is actually relevant to the code above, (12.2 [class.temporary])

and in _both_ versions of the standard it reads as follows

"""
3. When an implementation introduces a temporary object of a class that has a non-trivial constructor (12.1), it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with a non-trivial destructor (12.4). Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception.
"""

HTH.


(Clue - the answer's "Tom's right in at least one version of C++")

That would depend on how well you know the rules of the language.

PKB.

OTI

If you've never seen the above paragraph, you evidently don't
know enough about the language. You seem to be under the bizarre and
ill-founded misapprehension that there's only ever been one C++
standard, and therefore that all implementations conform to it.

I don't know about the comp.lang.c++ regulars, but if you were to waltz
into comp.lang.c and assert "the only C is C99", you'd be laughed out
of town immediately.

--
Richard Herring
.



Relevant Pages

  • Re: Any experience with "The Last One"?
    ... In this International Standard, the examples, the notes, the ... xconstructor ... xdestructor ...
    (comp.lang.c)
  • Re: Any experience with "The Last One"?
    ... In this International Standard, the examples, the notes, the ... xconstructor ... xdestructor ...
    (comp.programming)
  • Re: is such exception handling approach good?
    ... The C++ standard allows it. ... It's not good practice. ... Why it is not good code to call destructor at first before call placement ... if the caller of the constructor MyClass() is smart enough, ...
    (microsoft.public.vc.language)
  • Re: why Visual Studio can not optimize the initialization code?
    ... In the simple sample from MSDN, ... constructor of temporary object; ... destructor of temporary object; ...
    (microsoft.public.vc.language)
  • Re: std::map question
    ... > is constructed and then destructed; it's a temporary object. ... Yes I understood this after I posted but the class constructor is called ... My problem is that in my class CObject there is an Handle to an OS related ... CObject destructor. ...
    (comp.lang.cpp)