r/cpp•
grandstack

Advanced Apply, metaprogramming experiment

Got an idea for a different and more ergonomic variation of the apply function where you query the types with the lamda parameters, don't know if this has been done before. Would like to know what you think and if there's anything I can improve upon.

Where the tuples has a multiple of the same type the apply function gets the first occurence, if theres another lambda paramter with the same type it gets the second occurence in the tuple.

auto x = std::make_tuple(1, 2, 3, 4, 5, 6, 7, 8.0, 9, 10, 11, 33, 13, 14, 15.f, 16, 17, 18, 2);

std::cout << adv::apply(x, [](float a, double b, int c, int d) {

	std::cout << a << std::endl;
	std::cout << b << std::endl;
	std::cout << c << std::endl;
	std::cout << d << std::endl;

	return d;
}

// Output: 15.0, 8.0, 1, 2, 2

https://ideone.com/hJgGhy

Posted Apr 17, 2020, 12:14 AM UTC · edited

126
yuri-kilochekjourneyman template-wizard•

And what's the rationale?

1
[deleted]•

[deleted]

3

One suggestion I could think of:

You know the typenames at compile-time, so why not turn adv::apply into a macro that expands to std::apply?

That's my only bone to pick with this - I think it's a great idea, but I'd much rather it be distributed as a macro.

1
edstrange•

but I'd much rather it be distributed as a macro.

You don't hear that often.

8

That's why I switched to Rust!

0
LEpigeon888•

Could you explain a bit how it works ? I would like to understand but it's a bit hard for me to read all this template code.

1