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

前端 未结 4 1841
醉酒成梦
醉酒成梦 2020-12-06 18:23

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

4条回答
  •  清歌不尽
    2020-12-06 19:17

    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.

提交回复
热议问题