In C++20 we have designated initializers that are similar to C
They can be used to mimic named arguments:
struct Params
{
int speed;
bool optimize;
bool check;
};
void FooArgs(const Object& obj, int speed, bool optimize, bool check);
void Foo(const Object& obj, const Params ¶ms);
// the call:
Foo(obj, {.speed = 42, .optimize = true, .check = true});
The problem is that if you add a new data member to Params
you also need to remember to update all the callers. This is different from adding a new argument to FooArgs()
- where you'd get a compile error when you forget about the new argument...
Is there a way to enforce all data members are set in Params
? Ideally at compile-time.
The main problem I see is that designated initializers work with Aggregate types, so they cannot have user-defined constructors.