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

Concepts

Use case

Constrain template parameters for type safety.

Explanation

Concepts use readable constraints. The compiler error now says exactly which concept failed any why.

Code

#include <concepts>
#include <cstdint>
#include <iostream>
#include <type_traits>

template <typename T>
concept Unsigned = std::is_unsigned_v<T>;

template <Unsigned T> void printHex(T value) {
  std::cout << "0x" << std::hex << value << "\n";
}

int main() {
  uint64_t addr = 0x10001000;
  printHex(addr);

  /*
    concepts.cpp:x:x: error: no matching function for call to
        'printHex'
     xx |   printHex(-1);
        |   ^~~~~~~~
    concepts.cpp:x:x: note: candidate template ignored: constraints
        not satisfied [with T = int]
      x | template <Unsigned T> void printHex(T value) {
        |                            ^
    concepts.cpp:x:x: note: because 'int' does not satisfy 'Unsigned'
      x | template <Unsigned T> void printHex(T value) {
        |           ^
    concepts.cpp:x:x: note: because 'std::is_unsigned_v<int>'
        evaluated to false
      x | concept Unsigned = std::is_unsigned_v<T>;
        |                    ^
    1 error generated.
  */
  // printHex(-1);

  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/concepts
0x10001000