: On Sun, 25 Mar 2007 10:48:07 +0200, "Ivan Vecerina" wrote:
: >I rarely check for new/memory allocations in my code. I only do my
: >best to write code that is exception-safe. New-ing an object can
: >fail in any case because of a constructor failure.
:
: Yes, constructor failure is something you expect. It's entirely
: different from OOM, stack overflow, ...
As I previously pointed out, what you expect is dependent on
the platform and application that you are working on.
: >Given, push_back of a pointer is unlikely to fail, yet formally
: >it is a container operation that is allowed to fail and throw
: >an exception. So I'll write my code "to the spec".
:
: Allowed, but not required. So maybe the spec is defective?
You mean, maybe the experience of the designers of the C++
language and libraries pales in comparison to yours?
You mean, it should not be possible to write an allocator
that can fail to allocate memory on a platform with limited
resources, and throw an exception to allow graceful recovery?
: >The real issue is that it is illegal to create a
: >container of auto_ptr. vector<T*> is brittle by nature.
: >When I have a container of polymorphic objects, I find
: >that the overhead of vector< shared_ptr<T> > is acceptable.
:
: I don't think that 'smart pointes' solve any problem
Smart pointers (seek to) address the problem of memory leaks.
In your opinion, so many people have been working on smart
pointers for no reason ?
: (BTW, a container
: for auto_ptrs is not difficult but why should one use it?
: See:
http://www.relisoft.com/resource/auto_vector.html).
The difficulty of implementing a solution is proportional
to the breadth of its applicability.
Have you considered, for example, that:
- you'd have to rewrite a whole separate container
to have an auto_list
- it is unsafe to use auto_vector::iterator with
many standard algorithms, such as std::unique
I would suggest reading this introduction to R-value references
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm
[ here's a convenient index into more info about R-value
references and other C++ features that are in development:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2169.html ]
: >Depending on the type of device your code runs on, you will also
: >ensure that stack overflows can't be triggered by excessive
: >recursion, or ensure graceful failure if sufficient memory
: >isn't available for new incoming data.
:
: IMO, the question is which resources you can rely on withing your
: program and which not.
This is simple: what can be relied on or not, for portable C++ code,
is defined in the C++ standard. And according to this specification,
vector:

ush_back is allowed to throw an exception.
: Checking for stack overflow, OOM, ... would
: severely impair the creation of reusable components.
I have never suggested checking for stack overflow. But if I write
a recursive algorithms I will make sure, for example, that its
worst case recursion depth is logarithmic to the size of its input.
I do not specifically check for out-of-memory exceptions, but I use
RAII-related idioms to write code that is (hopefully) exception-safe
and immune to resource leaks.
: You must rely on some solid ground for your programming.
Isn't this exactly what I am doing by not making
platform-specific assumptions?
Take care -Ivan