I'd be particularly interested in eliminating having to use test, but a sequence like: [[ is parsed as introducing an attribute rather than using the return from a lambda as an index (and inserting white space doesn't seem to help). I could use .at() instead, but that imposes runtime overhead I'd also rather avoid.

Did you try extra paranthesis: funcs[([]...)]? Note that anything using a lambda in the index brackets is basically obfuscation.
–
NobodyJul 12 '14 at 19:25

@Nobody: Yes, I did, but couldn't find a way g++ would accept it.
–
Jerry CoffinJul 12 '14 at 19:26

As it is already ugly with the lambda you could get it to work with some arithmetic: funcs[0+[](int... and when talking about ugliness you could also use the explicit operator name: funcs.operator[]([]...
–
NobodyJul 12 '14 at 19:32

This is useful because it eliminates the overhead of memory allocations for a std::vector and the calls for constructor and destructor. As an embedded systems guy, I tend to think about whether code can be in ROM rather than RAM, and whether work can be done at compile-time rather than run-time.

Note, too that I've omitted the variable name for the last three lambdas. There's no real difference except that it prevents the compiler from generating a warning about "unused parameter i" in those three lines.

Also, I think you're overthinking the lambda. The test lambda isn't really necessary at all since you can call it like this:

funcs[ (i % 3 == 0) + 2 * (i % 5 == 0) ](i);

So (without xrange.h, although one could use that also) it would look like this:

I meant std::array, actually (sorry, I should've made that clear).
–
Jamal♦Jul 12 '14 at 21:26

@Jamal: std::array and a C-style array would be very similar in this context, but I used the C-style array just because it seems to me to be a clearer way to express the idea. Memory usage and performance would likely be identical, so we can attribute the choice to aesthetic preference.
–
EdwardJul 12 '14 at 21:43