Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

auto(x) (decay copy)

Use case

Create explicit copy in expressions.

Explanation

auto(x) creates a decayed prvalue copy. Useful in generic code.

Code

#include <algorithm>
#include <print>
#include <vector>

int main() {
  std::vector<uint64_t> addrs = {0x1050, 0x1010, 0x1040, 0x1020};

  // Before C++23: need temporary variable.
  // auto copy = data;

  // C++23: inline decay copy.
  auto sorted = [](auto v) {
    std::sort(v.begin(), v.end());
    return v;
  }(auto(addrs));

  std::println("Original first: {:#x}", addrs[0]);
  std::println("Sorted first: {:#x}", sorted[0]);

  return 0;
}

View on GitHub.

Output

$ ./src/c++23/build/auto-x-decay-copy
Original first: 0x1050
Sorted first: 0x1010