Boost logo

Boost :

Subject: Re: [boost] program_options custom validators
From: Vladimir Prus (vladimir_at_[hidden])
Date: 2009-07-13 02:11:04

Zachary Turner wrote:

>> add_options() ("test", value<int>()->validate(is_in_range(1, 10)) );

>> That is something I wanted to do for a while, but never found the time.

> Actually I was thinking about that too and yes that would be more
> useful. I'm generally opposed to customization by free-standing
> overloads because it means you can customize only once per type,
> rather than per instance. So I was trying to do it with the minimum
> possible amount of code changes. Is there a way to implement what you
> suggest without breaking existing code?

I think it's fairly easy to write the code so that if no validate is specified
on specific option, the free-standing one is called. However, if the decision
is made at runtime it means that free-standing validate will be instantiated
for every time, and it's a bit tricky to avoid.

In theory, the 'validate' call may return a special type that knows that validate
was specified. Then, operator(), if called with plain value_semantics, may instantiate
free-standing validate and associate it with the option. I did not try this,
so I don't know if that will be a mess.

- Volodya

Boost list run by bdawes at, gregod at, cpdaniel at, john at