Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-11-21 10:22:20


Daniel Wallin <dalwan01_at_[hidden]> writes:

> Rene Rivera wrote:
>> Dave, Daniel,
>> I have a use case which would be nice to support. I have some
>> functions which take some number of flag arguments, akin to:
>> void foo(const char * name, float value, bool flag1 = false, bool
>> flag2 = false)
>> {
>> std::cout << name << " = " << value << "\n";
>> }
>> And I can add the named arg interface, so I can use it as:
>> foo((value = 5.2f, name = "baz", flag1 = true));
>> But ideally I'd like the use to look like:
>> foo((value = 5.2f, name = "baz", flag1));
>> That is, the presence of the keyword is enough to set the argument
>> to true. Possible?
>
> Not with the current library, but it could very easily be added. Perhaps
> something like:
>
> bool flag1_ = args[flag1.implicit(true) | false];
>
> What do you think? Dave?

I like it. Implicit flags are optional by definition, right? Should

    bool flag1_ = args[flag1.implicit(true)];

be an error?

And since bools will be by far the preferred flag type, should we just
allow

    bool flag1_ = args[flag1.implicit];

??

And should flag-ness be part of the keywords type

  struct foo_keywords
    : boost::keywords<
          boost::flag<
              flag1_t
            , is_convertible<mpl::_, bool> // #0
>
        , value_t
>
  {};

So that we can accept

   foo(flag1)
   foo(value = 3)

and reject

   foo(value) // #1
   foo(flag1 = true) // #2

with SFINAE ??

Perhaps it is sufficient to reject #1 after overload resolution and
make #2 legal.

And should #0 be optional (since that would be the right default?)

> We could also just have all keywords have an implicit value true,
> but I think that might cause some problems for users.

Yeah, it might.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk