auto count = (std::size_t(0) + ... + (pred(ts) ? 1 : 0));
// expands to: std::size_t(0) + (pred(ts[0]) ? 1 : 0)
// + (pred(ts[1]) ? 1 : 0)
// + ...
My deepest sympathies to the person who has to fix bugs in that kind of codebase 10 years from now. > auto count = (std::size_t(0) + ... + (pred(ts) ? 1 : 0));
Wait that's literal, non-metasyntacic elipses? What the actual fuck!?And it will want revenge.
xs[0] + ... + xs[n]
so that's kind of cool... though C++ seems like an odd place to find it. i wonder how expressive it is, i.e. what the limitations are.NOTE: the following might be incorrect, i really don't know C++ well enough to tell...
unfortunately it looks like it's limited to working with "parameter packs", which i understand are basically varargs, and can't be used to fold/map e.g. a normal vector. which seems like a strange choice... if you're going to add this much syntax, why limit it like that?
This is essentially a QoL improvement for existing users of variadic templates, where unnecessary complexity was required to simulate the behavior fold expressions provide for packs.
though as mentioned, i don't really use C++ much. are there reasons why this wouldn't work well for run-time stuff?
---
¹ it'd have to be mindful of short-circuiting constructs, but compilers special-case those anyway
Then again, we already have for loops, range for loops, the ranges library, the std algorithms, the parallel algorithms... I don't think people would welcome yet another way of writing things, but who knows...
but does that really matter, as long it has well-defined semantics? even for the compile-time case, it's up to the compiler/optimizer to decide whether `(xs + ...)` ends up as multiple ADDs or one ADD in a loop – you're only guaranteed that it's observable behavior will be `xs[0] + xs[1] + xs[2] + ...`, no?
Also I'd bet a dollar that there are 100 lisp libs with this (published or not)