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

size_t literal suffix

Use case

Avoid signed/unsigned comparison warnings.

Explanation

0uz = std::size_t, 0z = signed version of std::size_t. Integer literals are explained here.

Code

#include <cstdint>
#include <print>
#include <vector>

int main() {
  std::vector<uint8_t> buffer(256);

  // Before C++23:
  // warning: comparison of integers of different signs
  // for (int i = 0; i < buffer.size(); ++i) {}

  // C++23
  for (auto i = 0uz; i < buffer.size(); ++i) {
    buffer[i] = static_cast<uint8_t>(i);
  }

  auto index = 10uz;

  std::println("{}", buffer[index]);

  return 0;
}

View on GitHub.

Output

$ ./src/c++23/build/size_t-literal-suffix
10