C++ Logo

std-discussion

Advanced search

Re: Some feedback on scope guards

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 17 Apr 2023 15:48:19 +0200
I missed one question.

pon., 17 kwi 2023 o 14:54 Andrey Semashev via Std-Discussion
<std-discussion_at_[hidden]> napisaƂ(a):
>
(...)
> >> How does this new operator interact with virtual functions and base
> >> classes? Do you allow the operator to be =deleted? If so, what does it
> >> mean? Is the object now indestructible? (Because whether there will be
> >> an exception or not is a runtime property, not compile-time.)
> >
> > This is not a problem as this operator only is applied to stack variables.
> > This is determined at compile time what type it is.
>
> What is not a problem?
>
> You can have a unique_ptr on the stack that points to a base class with
> a virtual destructor. Will the unique_ptr call `operator unwind` on the
> pointed object? Will this require modification of the pointed type and
> the final derived type of the pointed object?
>
> In relation to this, do these expressions call the new operator?
>
> * `delete p`, where p is a raw pointer
> * `p->~T()` or `r.~T()`, where p is a raw pointer and r is a reference
>
> This is important for types like optional and variant.
>
> > `=deleted` is good question, and I think it should be supported.
> > probably if you "ban" exceptions unwind, class could only be placed in
> > `noexcept`
> > functions, if you ban normal unwind then it can't be a local variable.
>
> You can have exceptions within noexcept functions. You just can't
> propagate them to the caller.
>
> The next question would be, should a type be considered trivially
> destructible, if it has a non-defaulted `operator unwind`?
>

Its still trivial, but from a "stack" perspective it has a "destructor".
Same as `foo(T x) noexcept;` is not `noexcept` as `T` can throw.
```
{
   T x;
   //have all unwind code like any other not trivial variable
   //x.operator normal_unwind(); //not trivial
   //x.~T(); //noop
}
```
In any other context this do not matter as:
```
delete ptr_x; //"call" only "noop" `~T()`
```

Received on 2023-04-17 13:48:32