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

std::to_array

Use case

Convert C array to std::array.

Explanation

std::to_array deduces size automatically. Useful for converting legacy C arrays.

Code

#include <array>
#include <cstdint>
#include <iostream>

int main() {
  // https://en.wikipedia.org/wiki/Mach-O
  uint8_t magic[] = {0xCF, 0xFA, 0xED, 0xFE};
  auto arr = std::to_array(magic);

  std::cout << "Size: " << arr.size() << "\n";

  // $ echo "mov x29, sp" | llvm-mc -arch=aarch64 -show-encoding
  //    mov     x29, sp                         // encoding: [0xfd,0x03,0x00,0x91]
  // $ echo "bl 0x40" | llvm-mc -arch=aarch64 -show-encoding
  //    bl      #64                             // encoding: [0x10,0x00,0x00,0x94]
  auto opcodes = std::to_array<uint32_t>({0x910003FD, 0x94000010});

  for (auto op : opcodes) {
    std::cout << "0x" << std::hex << op << "\n";
  }

  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/std-to_array
Size: 4
0x910003fd
0x94000010