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

Use case

Store arbitrary metadata on symbols.

Explanation

std::any stores any single value with type safety at access time. Unlike void*, it knows its type.

Code

#include <any>
#include <cstdint>
#include <iostream>
#include <map>
#include <string>

int main() {
  std::map<std::string, std::any> symInfo;

  symInfo.emplace("address", uint64_t{0x10001000});
  symInfo.emplace("name", std::string{"_main"});
  symInfo.emplace("isExported", true);

  std::cout << "Address: 0x" << std::hex
            << std::any_cast<uint64_t>(symInfo["address"]) << "\n";
  // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-builtin
  std::cout << "Address type: " << symInfo["address"].type().name() << "\n";
  std::cout << "Name: " << std::any_cast<std::string>(symInfo["name"]) << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/std-any
Address: 0x10001000
Address type: y
Name: _main