|
|
Subscribe / Log in / New account

GCC begins move to C++

GCC begins move to C++

Posted Jun 2, 2010 1:31 UTC (Wed) by dvdeug (guest, #10998)
In reply to: GCC begins move to C++ by dgm
Parent article: GCC begins move to C++

If you convert the C statement "a = b + c();" directly into C++, the only place where exceptions can come up is if c() suddenly throws exceptions. If the causes don't matter then make c() not throw exceptions--if you don't control the source, you can always add an inline wrapper around it. If it does matter, then for a straightforward conversion you don't need to change anything and both the C and C++ programs will do poorly defined, unpleasant things in the presence of errors.


to post comments

GCC begins move to C++

Posted Jun 2, 2010 13:53 UTC (Wed) by dgm (subscriber, #49227) [Link] (3 responses)

> If you convert the C statement "a = b + c();" directly into C++, the only place where exceptions can come up is if c() suddenly throws exceptions.

You would be surprised. I recommend you to read this http://www.gotw.ca/gotw/020.htm

In this concrete example:

1. c() can indeed throw an exception in the code, but can also propagate uncaught exceptions from any code called within, possibly including constructors.
2. + can be an overloaded operator, and thus can also throw.
3. = can need to create a temporary object, whose constructor can also throw.

AFAIK, the only thing that cannot throw is "b".

GCC begins move to C++

Posted Jun 2, 2010 14:37 UTC (Wed) by mjthayer (guest, #39183) [Link]

> In this concrete example:
> 1. c() can indeed throw an exception in the code, but can also propagate uncaught exceptions from any code called within, possibly including constructors.
> 2. + can be an overloaded operator, and thus can also throw.
> 3. = can need to create a temporary object, whose constructor can also throw.

I think the OP was talking about code directly converted from C, so those things should not apply.

GCC begins move to C++

Posted Jun 3, 2010 0:24 UTC (Thu) by dvdeug (guest, #10998) [Link] (1 responses)

If either = or + in a = b + c(); can throw exceptions, then it's not the same code. You're looking at something like

a = copy_foo (add_foo (b, c()));

in C. If C++ would have thrown an exception, then a will have an error code in it (and a will have to support error values, and that value will have to be checked just like you have to catch an exception), a will have trash in it (and you'll have to check it--if you can) or the whole thing will crash, like if copy_foo assumes that add_foo can't return errors or trash.

GCC begins move to C++

Posted Jun 3, 2010 16:31 UTC (Thu) by dgm (subscriber, #49227) [Link]

Exactly, remember that it could not be the case initially, but the project will hopefully continue evolving, and thus the assumptions that made the code correct will change.

That's basically the problem. Adding exceptions to an established code base changes basic assumptions programmers would probably have made (nobody in his right mind would lose time writing exception-safe code if your language has no exceptions!).


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds