How to workaround on this (bug?)

H. S. Teoh hsteoh at qfbox.info
Fri Sep 16 23:06:35 UTC 2022


On Fri, Sep 16, 2022 at 10:43:43PM +0000, frame via Digitalmars-d-learn wrote:
> ```d
> import std.variant;
> 
> // error: destructor `std.variant.VariantN!32LU.VariantN.~this` is not
> `nothrow`
> void fun(Variant v) nothrow
> {
> 
> }
> 
> void main()
> {
>    fun(Variant());
> }
> ```
> 
> A reference, pointer or slice works.

Yes, because in that case the dtor would not need to be invoked in .fun.


> I could do something on the caller site but the signature of `fun()`
> should remain like above.

I was doing to suggest using `const ref`, but if the signature must
remain the same, then I'm out of ideas.

Basically, if you pass something to .fun by value, then that value must
be destroyed by .fun once it's ready to return.  So if the value has a
dtor, the dtor must be called upon exiting from .fun.  Since Variant has
a throwing dtor, this means .fun may throw when it's about to return,
which violates `nothrow`.


T

-- 
If creativity is stifled by rigid discipline, then it is not true creativity.


More information about the Digitalmars-d-learn mailing list