7 ms·
Just FYI > but then you can't use `new` You can certainly use new without exceptions, see https://en.cppreference.com/w/cpp/memory/new/nothrow https://en.cppr
by spacechild1 6y ago
Just FYI
> but then you can't use `new`
You can certainly use new without exceptions, see https://en.cppreference.com/w/cpp/memory/new/nothrow https://en.cppreference.com/w/cpp/memory/new/nothrow or https://stackoverflow.com/a/15292148 https://stackoverflow.com/a/15292148
> and without constructors you can't use RAII, and so on).
I think RAII is more about destructors (which must never throw exceptions). You can certainly write RAII style without throwing constructors. Generally, exceptions depend on RAII, but not the other way round.
- tsimionescu 6y ago> You can certainly use new without exceptions, see https://en.cppreference.com/w/cpp/memory/new/nothrow https://en.cppreference.com/w/cpp/memory/new/nothrow or https://stackoverflow.com/a/15292148 https://stackoverflow.com/a/15292148 You can use new without exceptions, but you can't detect OOM then, if I'm not mistaken. > I think RAII is more about destructors (which must never throw exceptions). You can certainly write RAII style without throwing constructors. Generally, exceptions depend on RAII, but not the other way round. The whole idea of RAiI is that you Acquire a Resource in the constructor (during Initialization), so that the destructor can guarantee it gets de-allocated. If you construct your object and then acquire a resource in it later, from a function that can return an error without throwing, then you introduce complexity in the destructor and may risk errors more.
- qalmakka 6y ago> You can use new without exceptions, but you can't detect OOM then, if I'm not mistaken. AFAIK `new (std::nothrow) T` returns nullptr if allocation fails, like std::malloc.
- spacechild1 6y agoExactly! It's worth pointing out that 'new (std::nowthrow) T' itself also performs a NULL check, so it doesn't call the constructor if the allocation failed.
- spacechild1 6y ago> The whole idea of RAiI is that you Acquire a Resource in the constructor (during Initialization), so that the destructor can guarantee it gets de-allocated Yes, that's the meaning of the acronym means, but unfortunately it's really a misnomer. When we say RAII style programming, we rather mean scoped destruction to avoid manual resource management. A classic example would be using std::lock_guard over a raw std::mutex. Some RAII objects don't throw on resource acquisition failure (std::fstream) or they expect that resource acquisition can fail (std::unique_lock with std::try_to_lock tag). Other RAII objects might not require any resource in the constructor at al, instead their only job is to release the resource that is given to them, like std::unique_ptr.
- account42 6y ago> You can use new without exceptions, but you can't detect OOM then, if I'm not mistaken. You can detect the same errors with and without exceptions - the nothrow variant returns nullptr on error. Due to overcommit, neither can be relied on to detect OOM in practice. > If you construct your object and then acquire a resource in it later, from a function that can return an error without throwing, then you introduce complexity in the destructor and may risk errors more. Not neccessrily, e.g. free and delete are both no-ops for nullptr.