Skip to content

<type_traits>: More general _Is_trivially_swappable #2111

@frederick-vs-ja

Description

@frederick-vs-ja

Describe the bug

_Is_trivially_swappable still fails to detect many potentially trivially swappable types in std, e.g. std::reference_wrapper, std::basic_string_view, and std::span. Because ADL finds a std::swap overload in <utility> for them. Although for some types the results are corrected via #2044.

A case similar to #2039 can be seen at https://godbolt.org/z/aGbxhTdT8.

On the other hand, _Is_trivially_swappable doesn't handle array types now. Most algorithms don't accept element types that are arrays, but it seems that at least std::ranges::swap_ranges is required to support them, and vectorization may be available.

Expected behavior
Other (potentially) trivially swappable types in namespace std are detected by _Is_trivially_swappable. E.g. the following snippet should compile:

static_assert(std::_Is_trivially_swappable_v<std::reference_wrapper<int>>);
static_assert(std::_Is_trivially_swappable_v<std::string_view>);
static_assert(std::_Is_trivially_swappable_v<std::span<char>>);
static_assert(std::_Is_trivially_swappable_v<std::byte[42]>);

STL version
Microsoft Visual Studio Community 2022 Preview 1.1 Version 17.0.0

Additional context
I have written an improved implementation and tested it at godbolt (without uglification, on ver 19.28-19.30): https://gcc.godbolt.org/z/qKaP5WecE.

std::is_trivially_move_constructible_v<std::string_view> becomes false (which is wrong) on ver 19.30, which is probably caused by a compiler bug a pending LWG issue LWG-3581 (already accepted).

One-by-one fix may be needed for array, pair, tuple, optional, and variant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions