4

I have a function whose exact behaviour is controlled by a set of flags. Most of these are usually false. Having them all as parameters quickly becomes very messy.

I could create an enum for the flags:

enum ParseFlags { assignmentPossible = 1, commaDelimits = 2, isConst = 4 };
PExpression ParseExpr(unsigned int flags);

A bit better: now, when calling the function, I only have to specify the values I want to set, instead of a long list of bools.

However, I would like to know if there is a more elegant or more general way of passing a subset of options to a function. What if I have non-boolean options?

I'm looking for a C++ (Boost is ok) way to call a function like:

ParseExpr({ isConst: true; maxDepth: 5 });

where I can omit all values that I want to leave at their default.

What techniques and/or libraries exist for this?

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131

1 Answers1

5

Ripping from this library's design, you could have a parameter class which curries its set methods.

class ParseFlags
{
public:
    ParseFlags& assignable(bool b) {assignable_ = b; return *this;}
    ParseFlags& delimiter(char c) {delimiter_ = c; return *this;}
    ParseFlags& constness(bool b) {constness_ = b; return *this;}
private:
    bool assignable_ = false;
    char delimiter_ = ',';
    bool constness_ = false;
};

PExpression ParseExpr(const ParseFlags& = ParseFlags{});

Call it like

auto exp = ParseExpr(ParseFlags().constness(false).delimiter('.'));
auto exp2 = ParseExpr();

Which is almost as good as having named parameters.

Passer By
  • 19,325
  • 6
  • 49
  • 96
  • Currying is imho sth different, i.e creating a new function with one fixed argument by specifying one (of many) function arguments to a previously existing function. Otherwise, great answer and maybe the easiest technique. – Felix Dombek Jun 25 '17 at 23:46
  • @FelixDombek I agree, and I'd love to have named parameters in C++ too. I've searched high and low for something similar and this is the best I got. – Passer By Jul 02 '17 at 08:36