microsoft / microsoft/STL

`<type_traits>`: More general `_Is_trivially_swappable`

Open
#2,111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the linked Compiler Explorer case and reading the _Is_trivially_swappable implementation. Check the expected static assertions for reference_wrapper, string_view, span, and byte arrays, then verify the behavior for the additional array, pair, tuple, optional, and variant cases mentioned in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.