C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: Standardised Type Punning API for Wrapper Types

From: Josh Warren <xzaton.jw_at_[hidden]>
Date: Mon, 2 Sep 2024 18:21:03 +0100
>
> How would you know that the container specialization is different?


The template specialisation would need to resolve for wrapper<T>, and T.

e.g. in the case of `std::vector<bull>` vs `std::vector<bool>`, the
specialisations chosen would be

for `wrapper<T>`

template <class _Ty, class _Alloc = allocator<_Ty>>
class vector

for `T` (a.k.a bool)

template <class _Alloc>
class vector<bool, _Alloc>

Since the compiler knows which specialisations are chosen, and they're
different in this case, it would then emit an error when trying to type pun
`std::vector<bull>` to `std::vector<bool>`

At least each container would have to support such a facility to remove
> the type by itself. Otherwise the implementation details could hide
> incompatibilities.
>
> std::vector could support a move_to member function:
>
> std::vector<my_distance_type> d1;
>
> // creates a new d2 with new metadata, but keeps the convertible actual
> data without copying
>
> // d1 is empty afterwards
>
> std::vector<double> d2 = d1.move_to<double>();


The move is not the same as a reinterpret_cast, so this is slightly
different in terms of outcome.

my proposal <Scalar Inheritance> would allow sub-typing (instead of
> wrapping) scalar types. Would that be not a clearer and better solution?


I originally considered this syntax, and while I eventually settled on a
default conversion operator, I wouldn't be opposed to scalar inheritance.
However, one consideration I had was the potential to disable the
type-punning under certain circumstances which may be easier to
understand/write with a requires clause on the special member function.

On Mon, 2 Sept 2024 at 17:43, Karafiát, László via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hi,
>
> my proposal <Scalar Inheritance> would allow sub-typing (instead of
> wrapping) scalar types. Would that be not a clearer and better solution?
>
> László
>
> On 2024-09-02 17:07, Sebastian Wittmeier via Std-Proposals wrote:
> > How would you know that the container specialization is different?
> >
> > Or in other words they are always different as it is a template.
> >
> > At least each container would have to support such a facility to remove
> > the type by itself. Otherwise the implementation details could hide
> > incompatibilities.
> >
> > std::vector could support a move_to member function:
> >
> > std::vector<my_distance_type> d1;
> >
> > // creates a new d2 with new metadata, but keeps the convertible actual
> > data without copying
> >
> > // d1 is empty afterwards
> >
> > std::vector<double> d2 = d1.move_to<double>();
> >
> >
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-09-02 17:21:18