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

Use case

Call functions with tupple arguments.

Explanation

std::apply unpacks a tuple and calls a function with its elements as arguments. Simpler than index_sequence.

Code

#include <cstdint>
#include <iostream>
#include <tuple>

void printSegment(const char *name, uint64_t addr, uint64_t size) {
  std::cout << name << " @ 0x" << std::hex << addr << " (size: 0x" << size
            << ")\n";
}

int main() {
  auto seg = std::make_tuple("__TEXT", 0x10000000, 0x4000);

  std::apply(printSegment, seg);

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/std-apply
__TEXT @ 0x10000000 (size: 0x4000)