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

Use case

Uniformly call any callable.

Explanation

std::invoke calls any callable uniformly (functions, lambdas, member functions etc.).

Code

#include <cstdint>
#include <functional>
#include <iostream>

struct Analyzer {
  uint64_t base = 0x10000000;
  uint64_t getBase() { return base; }
};

int main() {
  auto toOffset = [](uint64_t addr, uint64_t base) { return addr - base; };

  std::cout << "Offset: 0x" << std::hex
            << std::invoke(toOffset, 0x10001000, 0x10000000) << "\n";

  Analyzer a;
  std::cout << "Base: 0x" << std::hex << std::invoke(&Analyzer::getBase, a)
            << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/std-invoke
Offset: 0x1000
Base: 0x10000000