11 Jan 2018 • C++ tricks: named function arguments

You all know what they are and why they're useful. In C you can hack it with designated initialisers and macros so your calls look like f( .x = 1, .y = 2 );. C++ doesn't have designated initialisers, but you can do something pretty similar with lambdas:

struct FooArgs { int x, y; };
int foo_impl( const FooArgs & args ) {
	return args.x + args.y;
}

#define foo( ... ) [&]() { FooArgs A; __VA_ARGS__; return foo_impl( A ); }()

int main() {
	return foo( A.x = 1, A.y = 2 );
}

This is junk but nice to know it can be done.