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::span

Use case

Non-owning view of contiguous memory.

Explanation

std::span replaces pointer+length pairs. Non-owning, works with any contiguous container.

Code

#include <cstdint>
#include <iostream>
#include <span>
#include <vector>

void hexDump(std::span<const uint8_t> data) {
  for (auto byte : data) {
    std::cout << std::hex << static_cast<int>(byte) << " ";
  }

  std::cout << "\n";
}

int main() {
  // https://github.com/apple-oss-distributions/xnu/blob/f6217f891ac0bb64f3d375211650a4c1ff8ca1ea/EXTERNAL_HEADERS/mach-o/loader.h#L84
  std::vector<uint8_t> vec{0xCF, 0xFA, 0xED, 0xFE};
  uint8_t arr[] = {0x00, 0x00, 0x00, 0x00};

  hexDump(vec);
  hexDump(arr);
  hexDump({vec.data(), 2});

  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/std-span
cf fa ed fe 
0 0 0 0 
cf fa