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

constexpr std::unique_ptr

Use case

Smart pointers in compile-time code.

Explanation

std::unique_ptr is now fully constexpr.

Code

#include <array>
#include <memory>

constexpr bool isValidMagic(uint32_t magic) {
  auto valid = std::make_unique<uint32_t[]>(2);

  // https://en.wikipedia.org/wiki/Mach-O
  valid[0] = 0xFEEDFACF;
  valid[1] = 0xFEEDFACE;

  for (int i = 0; i < 2; ++i) {
    if (valid[i] == magic)
      return true;
  }
  return false;
}

int main() {
  static_assert(isValidMagic(0xFEEDFACF), "Invalid magic.");
  return 0;
}

View on GitHub.

Output

$ ./src/c++23/build/constexpr-std-unique_ptr