4 ms·
to me concepts and coroutines really reduce the amount of boilerplate needed. - just the ability of doing if constexpr(requires { T::some_member; }) { ..
by jcelerier 4y ago
to me concepts and coroutines really reduce the amount of boilerplate needed.
- just the ability of doing
if constexpr(requires { T::some_member; }) { ... }
to check if a type has a member variable / function / whatever makes code infinitely clearer than the previous mess requiring overloads.
- coroutines finally enable to properly abstract the data structures used by a type's implementation, e.g. you don't need to spill out anymore that your type stores stuff in std::vector or std::array or boost::small_vector or std::list etc etc to client code, and they simplify async code very well. for instance with Qt: https://qcoro.dvratil.cz/reference/core/qprocess/#examples https://qcoro.dvratil.cz/reference/core/qprocess/#examples
- three-way comparison and automtic generation of comparison / equality function is really great for removing needless boilerplate
- void f(auto arg) { ... } instead of template<typename T> void f(T arg) { ... }
- foo{.designated = 123, .init = "456" }; (although it was already more or less supported on every relevant compiler for years
- 8n4vidtmkvmk 4y agodesignated initializers are a trap. you can't require certain fields are set... this has been a big pain point for my team. guess we should have used builder pattern or something
- pjmlp 4y agoI would say constructors are what should be used for such kind of compound initializations. C# is going the route of allowing required/optional fields in designated initializers, and from my point of view it is just a mess for what should be a constructor call.
- leni536 4y ago> you can't require certain fields are set There is a way, but it's cursed: https://godbolt.org/z/fzc6WEz5e https://godbolt.org/z/fzc6WEz5e
- ncmncm 4y agoClever. Wrap it in a concept, and it might have legs.
- nyanpasu64 4y agoSadly `if constexpr (requires {})` was not implemented in MSVC until VS2022, which was not even released when I tried using it and backed out because it wouldn't build on MSVC, and is still a new and less-adopted compiler than 2019: https://developercommunity.visualstudio.com/t/requires-clause-in-constexpr-if-doesnt-work/929394 https://developercommunity.visualstudio.com/t/requires-claus...