Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-02-14 04:43:51


> 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").

Personally I'd simplify one line like this:

//void some_operation(inputs, optional<vector<case>&> special_cases = none) {
void some_operation(inputs, vector<case>* special_cases = 0) {
    for (...)
    {
        ...
        if (special_case)
        {
            ...
            if (special_cases)
                special_cases->push_back(current_case);
        }
    }
}

That's what the language construct pointer is for. Basic C++: Use a ref when you it can't be null; use a pointer when it can:
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Sorry to pick on you Nate, but your example is just what I'd like to avoid. People redefining concepts that the language already has.

Any C++ programmer reading your code should understand what T* means. He may not be familiar with boost::optional<T&>.

Chris


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