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

Use case

Partial function application (bind first N args).

Explanation

std::bind_front binds leading arguments.

Code

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

void logMsg(const char *level, uint64_t addr, const char *msg) {
  std::cout << "[" << level << "] 0x" << std::hex << addr << ": " << msg
            << "\n";
}

int main() {
  auto logError = std::bind_front(logMsg, "ERROR");
  auto logInfo = std::bind_front(logMsg, "INFO");

  logError(0x10001000, "Invalid instruction.");
  logInfo(0x10001000, "Entry point.");

  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/std-bind_front
[ERROR] 0x10001000: Invalid instruction.
[INFO] 0x10001000: Entry point.