|
Boost : |
From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-01-21 18:47:37
Brian McNamara wrote:
> Is there an effective way inside an "_impl" function to "ask" if a
> parameter was supplied with a value? I can maybe imagine wanting to use
> this library to let me do both:
>
> make_point( x = 3.3, y = 4.4 );
> make_point( theta = 45, r = 3.0 );
>
> but it seems like the make_point_impl() logic would have to go
>
> if( user supplied a value for x, then )
> makeXYpoint( params[x], params[y] )
> else
> makeRTpoint( params[r], params[theta] )
>
> but it's unclear to me if the interface allows me to ask the question in
> the if() statement above.
params[x] will give a compilation error if the parameter wasn't supplied
by the user, unless it has a default.
You can do something like this:
struct unspecified {};
void make_point_impl2(float x, float y, unspecified, unspecified);
void make_point_impl2(unspecified, unspecified, float r, float theta);
template<class Params>
void make_point(Params const& p)
{
make_point_impl2(
p[x | unspecified()]
, p[y | unspecified()]
, p[r| unspecified()]
, p[theta | unspecified()]
);
}
Best I could come up with right now.
> The example in the documentation (Section 2's foo()) was nice and
> simple, but the examples in the zip file look more complicated and
> harder to follow. It'd be nice to have the same foo() example in the
> docs as (another) one of the distibuted test files.
Yeah that's probably because the code in the zip isn't intended to be
used as examples, but tests. You are right though, we could use more
examples.
Thanks,
-- Daniel Wallin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk