Loading...
Loading...
&#32; submitted by &#32; <a href="https://www.reddit.com/user/friedkeenan"> /u/friedkeenan </a> <br/> <span><a href="https://friedkeenan.github.io/posts/exploring-mutable-consteval-state/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/programming/comments/1rputf4/exploring_mutable_consteval_state_in_c26/">[comments]</a></span>
C++26 now allows structured binding declarations directly in conditions (if/while), per proposal P0963R3. The initializer is evaluated first, then its operator bool() decides the branch, and the individual bindings are available in both if and else scopes. This extends existing init-statement semantics to structured bindings; the decomposed object must be contextually convertible to bool. The change complements other C++26 enhancements to structured bindings—attributes on bindings, constexpr bindings, and parameter-pack bindings—making code that returns status-plus-data (e.g., std::expected-like types, optional/result patterns) more expressive and readable. Examples show concise error handling using bindings in if conditions.
C++26 now permits structured binding declarations directly in conditions (if/while), via proposal P0963R3. The initializer is evaluated first, then the condition uses the object's operator bool(), and the individual bindings are available in both the then and else branches. Other C++26 structured-binding enhancements include attribute annotations on individual bindings, constexpr bindings, and parameter-pack bindings. This change smooths common patterns where functions return a status plus payload or diagnostics (e.g., optional/expected-like types), making code more expressive and readable while following existing init-statement rules. The decomposed object must be contextually convertible to bool, otherwise the condition is ill-formed.
C++26: Structured bindings in conditions
&#32; submitted by &#32; <a href="https://www.reddit.com/user/friedkeenan"> /u/friedkeenan </a> <br/> <span><a href="https://friedkeenan.github.io/posts/exploring-mutable-consteval-state/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/programming/comments/1rputf4/exploring_mutable_consteval_state_in_c26/">[comments]</a></span>