C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Conditional copy ctor for std::map / std::unordered_map

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 28 Feb 2023 16:00:37 +0100
wt., 28 lut 2023 o 15:25 Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Mon, Feb 27, 2023 at 10:37 PM Mingxin Wang via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> When instantiating `std::map` or `std::unordered_map`, the value type could be non-copyable (but movable). However, both class templates have copy ctors and assignments. I do not see any wording suggesting they shall be deleted like `std::optional` does ([optional.ctor] Remarks: This constructor is defined as deleted unless is_copy_constructible_v<T> is true. If is_trivially_copy_constructible_v<T> is true, this constructor is trivial.) Shall we refine the wording here?
>
>
> https://quuxplusone.github.io/blog/2020/02/05/vector-is-copyable-except-when-its-not/
>

Hmm, why not have your cake and eat it too? We do not know if the type
is copyable, but we can ask the user for that:

```
class Foo;

namespace std
{

template<class T>
struct enable_container_copy : is_copy_constructible<T>
{

};

template<>
struct enable_container_copy<Foo>
{
    constexpr static bool value = false;
};

}

class Foo
{
    std::vector<Foo> bar; // vector check `enable_container_copy` not
`is_copy_constructible`
};
static_assert(std::enable_container_copy_v<Foo> ==
std::is_copy_constructible_v<Foo>); //sanity check
```

`enable_container_copy` can be specialized by the user to control
containers for incompatible classes.
Code like this can work in current compilers, only we need to mark
each special member with this proxy trait.

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

Received on 2023-02-28 15:00:50