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

Variable templates

Use case

Architecture specific constants.

Explanation

Variable templates allow templated constants. Cleaner than static class members or constexpr functions.

Code

#include <cstdint>
#include <iostream>

template <typename T> constexpr T pageSize = 0x1000;

template <typename T> constexpr T machoBase = 0;

// template<> marks an explicit specialization (for type uint64_t).
template <> constexpr uint64_t machoBase<uint64_t> = 0x10000000;

// template<> marks an explicit specialization (for type uint32_t).
template <> constexpr uint32_t machoBase<uint32_t> = 0x1000;

int main() {
  std::cout << std::hex;
  std::cout << "Page size: 0x" << pageSize<uint64_t> << "\n";
  std::cout << "64-bit base: 0x" << machoBase<uint64_t> << "\n";
  std::cout << "32-bit base: 0x" << machoBase<uint32_t> << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++14/build/variable-templates
Page size: 0x1000
64-bit base: 0x10000000
32-bit base: 0x1000