1 minute read

C++ is on its way to get a placeholder variable _ similar to other languages, e.g., Python, rust.

This is useful in multiple scenarios:

  • Variables used just for their side effects do not require a unique name anymore, e.g. locks, and other RAII objects.
    std::lock_guard someUniqueName(mutex); // old: needed to find unique name
    std::lock_guard _(mutex); // new: no name needed anymore
    
  • When unpacking objects via structured binding some parts might not be of interest. This can now be represented directly in code.
    [[maybe_unused]] auto [x, y, zUnused] = f(); // old
    auto [x, y, _] = f(); // new
    
  • Supposedly useful for pattern matching.

Implementation available in gcc14 and clang18 (both unreleased trunk versions).

Resources: