C++: Placeholder _
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:
- Short and precise explanation by Jason Turner’s cppweekly on youtube.
- Original paper: Placeholder variables with no name
- cppreference has no separate page as of this writing but the feature is listed on its C++26 overview page.