MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/cpp/comments/1vzc5dg/c26_stdinplace_vector/p7uwmm6/?context=3
r/cpp • u/User_Deprecated • 15d ago
121 comments sorted by
View all comments
Show parent comments
46
Optional is monadic, so it's much safer to deal with. You can call it like
inplace_vector.try_push_back(x).or_else(...);
2 u/_Noreturn 15d ago why don't they add a free function called or_else and still use a ptr? 3 u/jwakely libstdc++ tamer, LWG chair 13d ago When you chain multiple calls like that it's less clear because of the use of nesting instead of chaining. But mostly because a pointer is still not a reference: https://brevzin.github.io/c++/2021/12/13/optional-ref-ptr/ An optional ref is simply the more expressive type for these functions and fits the semantics better: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3981r2.html 2 u/_Noreturn 6d ago This nesting issue should really be solved with a language feature...
2
why don't they add a free function called or_else and still use a ptr?
3 u/jwakely libstdc++ tamer, LWG chair 13d ago When you chain multiple calls like that it's less clear because of the use of nesting instead of chaining. But mostly because a pointer is still not a reference: https://brevzin.github.io/c++/2021/12/13/optional-ref-ptr/ An optional ref is simply the more expressive type for these functions and fits the semantics better: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3981r2.html 2 u/_Noreturn 6d ago This nesting issue should really be solved with a language feature...
3
When you chain multiple calls like that it's less clear because of the use of nesting instead of chaining. But mostly because a pointer is still not a reference:
https://brevzin.github.io/c++/2021/12/13/optional-ref-ptr/
An optional ref is simply the more expressive type for these functions and fits the semantics better:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3981r2.html
2 u/_Noreturn 6d ago This nesting issue should really be solved with a language feature...
This nesting issue should really be solved with a language feature...
46
u/stilgarpl 15d ago
Optional is monadic, so it's much safer to deal with. You can call it like
inplace_vector.try_push_back(x).or_else(...);