Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: paul Fultz (pfultz2_at_[hidden])
Date: 2012-02-14 11:07:17


One disadvantage of using a pointer instead of optional<T&> is when I write a functions to serach for an item in a map like this: template<class T, class Map> optional<T&> search_for(Map& m, T& key); Optional lets me call get_value_or() so I can return a value if it cant find the key, but using a pointer I can't do that so easily. However, another class such as an optional_ref<T> class, could be created that would act like a pointer in this case, but still give the get_value_or() capability. Ultimately, I think optional should be a range(as well as the possible optional_ref<T>). I think it integrates better with C++. Instead of a get_value_or() function there would be a first_or() or a front_or() function that retrieves the first value from the range or returns something else when its empty. Also, checking for the value and getting the value could be done in one for loop. Now perhaps, its too late in the game for these things to be changed. Either way, optional<T> should be able to be made a range not intrusively. ----- Original Message ----- > From: "Hite, Christopher" <Christopher.Hite_at_[hidden]> > To: Boost Developers Mailing List <boost_at_[hidden]> > Cc: > Sent: Tuesday, February 14, 2012 4:43 AM > Subject: Re: [boost] [optional] generates unnessesary code for trivial types > >> 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 > > _______________________________________________ > Unsubscribe & other changes: > http://lists.boost.org/mailman/listinfo.cgi/boost >


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