Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-02-13 16:25:09


> From: akrzemi1_at_[hidden]
>
> Speaking for myself, I do not have a need for optional reference. I have
> been using Boost.Optional for many years, I find it extremely useful; and I
> never had a use case for optional reference. On the other hand, others seem
> to be in need of optional references.Andrey said in this thread that that
> he wants to use it. The only two motivating use cases that triggered the
> addition of optional references (that I am aware of) is the interface of
> Boost.Variant (see http://lists.boost.org/Archives/boost/2003/02/44585.php)
> and the use case for Boost.Spirit (see
> http://lists.boost.org/Archives/boost/2003/10/55584.php). I would be
> reluctant to just discard these use cases. On the other hand, if they are
> allowed, the controversy of assignment arises.

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)
{
    for (...)
    {
        ...
        if (special_case)
        {
            ...
            if (special_cases)
                special_cases->push_back(current_case);
        }
    }
}

Before I discovered optional, I used a plain reference, but that was annoying
because I had to create a dummy vector to be used as the default argument.
(The other alternative would have been to use a pointer, but then the caller
has to use the uglier syntax of passing in "&special_cases" rather than
"special_cases").

Regards,
Nate
                                               


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