C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Idea: moved_from<T>() for Efficient Moved-From State Construction

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Thu, 24 Apr 2025 17:34:13 +0200
czw., 24 kwi 2025 o 16:21 Jonathan Wakely via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On Thu, 24 Apr 2025 at 14:57, Breno Guimarães via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > If the author of the class needs to implement the specialization for moved_from<T>, they might very well implement a constructor that takes a tag indicating it's a type for overwrite.
> > I don't get why this facility would help in any way
>
> Exactly. If you are able to modify the class to support a possible
> change to the language which might happen one day, you could modify
> the class today to not require such a change to the language. If you
> can't add such a constructor, you wouldn't be able to take advantage
> of a hypothetical language change either.

Technically if moved object can be copied we can today create moved
from objects using static object like:
```
T moved_from()
{
    static T dummy = ([]{ T temp; T temp2 = std::move(temp); return temp; })();
    return dummy;
}
```
Of course we will pay the cost of locks on `static` (that could cost
more than `T()` :D ).

This only shows variability of implementation but not if we should do
it, default constructor should
be fast or use tag constructor to achieve that goal.


On the other hand, for optional in structs, could it be possible to
have langage level embed optional members or unions in struct that
share "control block"? that one `byte` effectively can handle 8
optional members in struct without increasing size too much.

```
struct Optional
{
    union keyword_tagged_union {
        T f;
    };
};

struct Variant
{
    union keyword_tagged_union {
        T1 f1;
        T2 f2;
        T3 f3;
    };
};
```
`keyword_tagged_union` will store state in hidden bit field in outer class.
For simple cases this will not grain lot over `bool enabled;`
but if you have 32 optionals and couple of unions in one class this will
benefit a lot.

Only drawback will need some special keyword to check this hidden field.
but it could simply make all union members accessed like pointer:
```
Variant v = foo();
if (v.f1) bar(*v.f1);
```

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-04-24 15:34:26