I'm converting some code from using CreateProcess
to use boost-process instead. I need to replace my CreateProcess
usages with boost::process::child
. The problem is they have incompatible ways for me to say "I want to use the default value". What used to be a one line command has become sixteen if
statements.
The function I currently use works like this (simplified):
void CreateProcess(int, float);
Each parameter had a value you could use to indicate you wanted the default. I'll use 0 in this example:
int int_param = ...;
float float_param = ...;
CreateProcess(0, 0); //Use both defaults
CreateProcess(int_param, 0); //Use default float
CreateProcess(0, float_param); //Use default int
CreateProcess(int_param, float_param); //No defaults
This is a common design. You know what I am talking about. Whether I wanted to use the default value or not could be decided with a simple conditional, such as (... ? 0 : int_param)
. This allowed every CreateProcess
call to be a single line of code.
CreateProcess( (... ? 0 : int_param), (... ? 0 : float_param) );
Where I used to call CreateProcess
, I now want to create a child
class. The constructor for child
works like this:
template<typename ...Args>
explicit child(Args&&...args);
Instead of passing a specific value to use a default, I have to pass nothing.
int int_param = ...;
float float_param = ...;
child c; //Use both defaults
child c(int_param); //Use default float
child c(float_param); //Use default int
child c(int_param, float_param); //No defaults
child c(float_param, int_param); //Argument order is irrelevant
So far my only "solution" is to use if
branches.
if (...) {
if (...)
child c;
else
child c(float_param);
} else {
if (...)
child c(int_param);
else
child c(int_param, float_param);
}
This example has only two possible arguments and it becomes four branches. The real child
has four possible arguments, so every instance has sixteen branches.
What I would like is some way of building the call to avoid this branching. A solution specific to boost::process
is also fine.
FYI, the last two arguments to child
may or may not be the same type.