[optional] proposal for extension

In the rationale of Boost.Optional, optional<T> is proposed as a convenient replacement of the time-honored practice of returning a std::pair<T,bool> with the bool indicating whether the T object is meaningful or not. Notably, this is precisely what some STL containers' memfuns do. So why not augment optional<T> with the following: optional<T>::optional(std::pair<T,bool> const&); and the obvious semantics? This would allow for instance to use optional<> like this: std::set<int> s; ... boost::optional<int> oi=s.insert(10); // oi is initialized iff insertion took place. Comments? Apologies if this has been discussed previously. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Pavel Vozenilek ha escrito:
"JOAQUIN LOPEZ MU?Z" <joaquin@tid.es> wrote
std::set<int> s; ... boost::optional<int> oi=s.insert(10); // oi is initialized iff insertion took place.
set::insert returns pair of iterator + bool.
You're right. I meant optional<iterator>. Best, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

"Joaquín Mª López Muñoz" <joaquin@tid.es> wrote in message news:4069B0EE.598AA848@tid.es...
Pavel Vozenilek ha escrito:
"JOAQUIN LOPEZ MU?Z" <joaquin@tid.es> wrote
std::set<int> s; ... boost::optional<int> oi=s.insert(10); // oi is initialized iff insertion took place.
set::insert returns pair of iterator + bool.
You're right. I meant optional<iterator>.
Also, the iterator produced by set::insert is always meaningful. If the insertion is successful then the iterator points to the just-inserted element, and the bool equals true. If the insertion is not successful then the iterator points to the element of the set that caused the insertion to fail, and the bool equals false. Joe Gottman

On Mon, 29 Mar 2004 23:23:24 +0200, JOAQUIN LOPEZ MU?Z <joaquin@tid.es> wrote:
In the rationale of Boost.Optional, optional<T> is proposed as a convenient replacement of the time-honored practice of returning a std::pair<T,bool> with the bool indicating whether the T object is meaningful or not.
It's not that honored, I think. At least once you know Barton-Nackman's Fallible ;) Genny.

JOAQUIN LOPEZ MU?Z wrote:
In the rationale of Boost.Optional, optional<T> is proposed as a convenient replacement of the time-honored practice of returning a std::pair<T,bool> with the bool indicating whether the T object is meaningful or not. Notably, this is precisely what some STL containers' memfuns do.
So why not augment optional<T> with the following:
optional<T>::optional(std::pair<T,bool> const&);
and the obvious semantics? This would allow for instance to use optional<> like this:
std::set<int> s; ... boost::optional<int> oi=s.insert(10); // oi is initialized iff insertion took place.
Comments? Apologies if this has been discussed previously.
Hi, Others have pointed out that the "bool" there doesn't mean ".first" is uninitialized, but anyway I like your idea :-) There are interfaces which do use pair<T,bool> with .second indicating if .first actually exist, so I see your point. Can anybody think of a reason for NOT adding it? If no one complains, I'll add it. Fernando Cacciola SciSoft
participants (6)
-
Fernando Cacciola
-
Gennaro Prota
-
JOAQUIN LOPEZ MU?Z
-
Joaquín Mª López Muñoz
-
Joe Gottman
-
Pavel Vozenilek