Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2012-02-14 13:00:02

Den 14-02-2012 10:43, Hite, Christopher skrev:
>> 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);
> }
> }
> }

I have a similar use-case, where an optional<std::exception&> is used as
an argument, and its important to preserve reference semantics.

> 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:
> 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&>.

The main benefit is that optional<T&> is /way/ more clear than T*;
people use T* even when it can't be null. In such a code base, T* is
ambiguous to read, whereas optional<T&> is not.

And no, we can't just clean up hundreds of k lines of source code to
make the interpretation unambiguous.

Anyway, it should be trivial to make a new optional<T&> as efficient as T*.


Boost list run by bdawes at, gregod at, cpdaniel at, john at