Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cleanly specify which arguments I am passing and which remain default?

Asked because of this: Default argument in c++

Say I have a function such as this: void f(int p1=1, int p2=2, int p3=3, int p4=4);

And I want to call it using only some of the arguments - the rest will be the defaults.

Something like this would work:

template<bool P1=true, bool P2=true, bool P3=true, bool P4=true>
void f(int p1=1, int p2=2, int p3=3, int p4=4);
// specialize:
template<>
void f<false, true, false, false>(int p1) {
  f(1, p1);
}
template<>
void f<false, true, true, false>(int p1, int p2) {
  f(1, p1, p2);
}
// ... and so on. 
// Would need a specialization for each combination of arguments
// which is very tedious and error-prone

// Use:
f<false, true, false, false>(5); // passes 5 as p2 argument

But it requires too much code to be practical.

Is there a better way to do this?

like image 281
Pubby Avatar asked Nov 18 '11 06:11

Pubby


4 Answers

Use the Named Parameters Idiom (→ FAQ link).

The Boost.Parameters library (→ link) can also solve this task, but paid for by code verbosity and greatly reduced clarity. It's also deficient in handling constructors. And it requires having the Boost library installed, of course.

like image 147
Cheers and hth. - Alf Avatar answered Oct 21 '22 14:10

Cheers and hth. - Alf


Have a look at the Boost.Parameter library.

It implements named paramaters in C++. Example:

#include <boost/parameter/name.hpp>
#include <boost/parameter/preprocessor.hpp>
#include <iostream>

//Define
BOOST_PARAMETER_NAME(p1)    
BOOST_PARAMETER_NAME(p2)
BOOST_PARAMETER_NAME(p3)
BOOST_PARAMETER_NAME(p4)

BOOST_PARAMETER_FUNCTION(
                         (void),
                         f,
                         tag,
                         (optional            
                          (p1, *, 1)
                          (p2, *, 2)
                          (p3, *, 3)
                          (p4, *, 4)))
{
    std::cout << "p1: " << p1 
            << ", p2: " << p2
            << ", p3: " << p3
            << ", p4: " << p4 << "\n";
}
//Use
int main()
{
    //Prints "p1: 1, p2: 5, p3: 3, p4: 4"
    f(_p2=5);
}
like image 42
Mankarse Avatar answered Oct 21 '22 14:10

Mankarse


Although Boost.Parameters is amusing, it suffers (unfortunately) for a number of issues, among which placeholder collision (and having to debug quirky preprocessors/template errors):

BOOST_PARAMETER_NAME(p1)

Will create the _p1 placeholder that you then use later on. If you have two different headers declaring the same placeholder, you get a conflict. Not fun.

There is a much simpler (both conceptually and practically) answer, based on the Builder Pattern somewhat is the Named Parameters Idiom.

Instead of specifying such a function:

void f(int a, int b, int c = 10, int d = 20);

You specify a structure, on which you will override the operator():

  • the constructor is used to ask for mandatory arguments (not strictly in the Named Parameters Idiom, but nobody said you had to follow it blindly), and default values are set for the optional ones
  • each optional parameter is given a setter

Generally, it is combined with Chaining which consists in making the setters return a reference to the current object so that the calls can be chained on a single line.

class f {
public:
  // Take mandatory arguments, set default values
  f(int a, int b): _a(a), _b(b), _c(10), _d(20) {}

  // Define setters for optional arguments
  // Remember the Chaining idiom
  f& c(int v) { _c = v; return *this; }
  f& d(int v) { _d = v; return *this; }

  // Finally define the invocation function
  void operator()() const;

private:
  int _a;
  int _b;
  int _c;
  int _d;
}; // class f

The invocation is:

f(/*a=*/1, /*b=*/2).c(3)(); // the last () being to actually invoke the function

I've seen a variant putting the mandatory arguments as parameters to operator(), this avoids keeping the arguments as attributes but the syntax is a bit weirder:

f().c(3)(/*a=*/1, /*b=*/2);

Once the compiler has inlined all the constructor and setters call (which is why they are defined here, while operator() is not), it should result in similarly efficient code compared to the "regular" function invocation.

like image 4
Matthieu M. Avatar answered Oct 21 '22 14:10

Matthieu M.


This isn't really an answer, but...

In C++ Template Metaprogramming by David Abrahams and Aleksey Gurtovoy (published in 2004!) the authors talk about this:

While writing this book, we reconsidered the interface used for named function parameter support. With a little experimentation we discovered that it’s possible to provide the ideal syntax by using keyword objects with overloaded assignment operators:

f(slew = .799, name = "z");

They go on to say:

We’re not going to get into the implementation details of this named parameter library here; it’s straightforward enough that we suggest you try implementing it yourself as an exercise.

This was in the context of template metaprogramming and Boost::MPL. I'm not too sure how their "straighforward" implementation would jive with default parameters, but I assume it would be transparent.

like image 2
Keith Layne Avatar answered Oct 21 '22 14:10

Keith Layne