|
Boost : |
Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-02-13 17:17:56
> From: lucanus.j.simonson_at_[hidden]
>
> Nathan Ridge wrote:
>
> >I use optional references in my code, in cases like the following:
> >I have an operation, which processes some elements, and some of the elements constitute special cases. The caller of the operation may or may not want to know about the special cases that arose, so they can optionally pass >in a container which will be populated with the special cases:
> >
> >void some_operation(inputs, optional<vector<case>&> special_cases = none) ;
>
> That's an interesting use case.
>
> Default arguments are equivalent to overloading. You could write it as two functions:
>
> void some_operation(inputs, vector<case>& special_cases, bool optimize_out_special_cases = false) ;
>
> void some_operation(inputs) {
> vector<case> dummy;
> some_operation(inputs, dummy, true);
> }
>
> I think it would be more convenient for the users to use the overloaded functions over wrapping their vector in an optional.
But they don't need to wrap their vector in an optional! That's the beauty of it.
They can just do:
vector<case> special_cases;
some_operation(inputs, special_cases);
if they care about the special cases, or:
some_operation(inputs);
if they do not.
And given that it's all the same to the users, it's more convenient for *me* to just *write*
one function.
Regards,
Nate
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk