[optional] std::tr2::optional

Hi, I use boost::optional quite often, find it very useful, and cannot imagine that it could not get into TR2. I am considering writing a proposal for adding it myself (if it is possible to propose things without attending the committee meetings), but I would first want to know how boost::optional could be improved given the new features in C++11. I provide a list of modifications that I could think of. What is your opinion? * Conversion explicit conversion to bool should be declared as explicit. * Should exception safety guarantees for assignment operations be enforced? In boost::optional they only provide a basic guarantee. One option is to provide strong guarantee for types T which implement operator= with strong guarantee, and give basic guarantee for other cases Other option (not sure if it is possible) is to provide strong guarantee for all cases at the cost of run-time performance (heap allocation). * Add move operations optional<T> & operator=( optional<T> && ) noexcept( nothrow_move_constructible<T>::value ); optional<T> & operator=( T && ) noexcept( nothrow_move_constructible<T>::value ); * move operations for optional references? But I am not sure what optional references are for... * What are optional references for anyway? * it could add an rvalue reference overload for stored object access: T&& operator*( ) && However using this overload implies that the user is not checking if an optional has been initialized, so it may encourage a bad practice. * Are in-place factories necessary if we have perfect forwarding? Instead, we can provide a reset function and a "variadic constructor": template< typename ... Args > void reset( Args&& ...args ); template< typename ... Args > optional( Args&& ...args ); The constructor will clash with another optional constructor: optional<T>::optional( bool cond, const T& val ); but perhaps it is not needed if we have std::optional<T> std::make_optional( bool cond,const T& val ); and move construction of optionals? * noexcept default construction and destroy operations * constexpr default ctor * Use nullptr instead of boost::none? - this will make some uses of optional pointers harder, but will make usage more clear in other cases. Regards, &rzej

On Friday, November 18, 2011 16:24:35 Andrzej Krzemienski wrote:
* Use nullptr instead of boost::none? - this will make some uses of optional pointers harder, but will make usage more clear in other cases.
I don't think nullptr is a good choice here because of the complications you mentioned. Actually, I would prefer traditional clear() and empty() methods for clearing and testing for value presence. The operator safe_bool() and operator!() may also be present in the interface for brevity in conditional expressions.

Le 18/11/11 16:24, Andrzej Krzemienski a écrit :
Hi, I use boost::optional quite often, find it very useful, and cannot imagine that it could not get into TR2. I am considering writing a proposal for adding it myself (if it is possible to propose things without attending the committee meetings), but I would first want to know how boost::optional could be improved given the new features in C++11. I provide a list of modifications that I could think of. What is your opinion? Hi,
good idea.
* Conversion explicit conversion to bool should be declared as explicit.
Doesn't an explicit conversion disallows to use it in a conditional expression as in if(opt) {}
* Should exception safety guarantees for assignment operations be enforced? In boost::optional they only provide a basic guarantee. One option is to provide strong guarantee for types T which implement operator= with strong guarantee, and give basic guarantee for other cases Other option (not sure if it is possible) is to provide strong guarantee for all cases at the cost of run-time performance (heap allocation). Provide the same exception guaranties as the T assignment seems the more reasonable. * Add move operations optional<T> & operator=( optional<T> && ) noexcept( nothrow_move_constructible<T>::value ); optional<T> & operator=( T&& ) noexcept( nothrow_move_constructible<T>::value ); +1 * move operations for optional references? But I am not sure what optional references are for...
* What are optional references for anyway?
* it could add an rvalue reference overload for stored object access: T&& operator*( )&& However using this overload implies that the user is not checking if an optional has been initialized, so it may encourage a bad practice.
* Are in-place factories necessary if we have perfect forwarding? Instead, we can provide a reset function and a "variadic constructor":
template< typename ... Args> void reset( Args&& ...args );
template< typename ... Args> optional( Args&& ...args ); I like them. The constructor will clash with another optional constructor: optional<T>::optional( bool cond, const T& val );
I don't understand why.
but perhaps it is not needed if we have std::optional<T> std::make_optional( bool cond,const T& val ); and move construction of optionals?
* noexcept default construction and destroy operations +1
* constexpr default ctor +1 * Use nullptr instead of boost::none? - this will make some uses of optional pointers harder, but will make usage more clear in other cases.
I have nothing against none. Optional is not a pointer and initializing it with nullptr could induce the user to think that it could be one pointer. Best, Vicente

I provide a list of modifications that I could think of. What is your opinion? This is more of an addition to your list, but I think adding
the | operator for getting the optional value or a default value would be nice. For example: optional<double> safe_sqrt(double x) { if (x < 0) return none; else return sqrt(x); } double x = safe_sqrt(4) | 0; //x = 4 double y = safe_sqrt(-1) | 0; //y = 0 I dont know if this is possible for tr2, but would be really nice addition in the future. Especially, since a lot of times i just want a default value if the optional value doesn't exist.

on Fri Nov 18 2011, paul Fultz <pfultz2-AT-yahoo.com> wrote:
I provide a list of modifications that I could think of. What is your opinion? This is more of an addition to your list, but I think adding
the | operator for getting the optional value or a default value would be nice. For example:
optional<double> safe_sqrt(double x) { if (x < 0) return none; else return sqrt(x); }
double x = safe_sqrt(4) | 0; //x = 4 double y = safe_sqrt(-1) | 0; //y = 0
I dont know if this is possible for tr2, but would be really nice addition in the future. Especially, since a lot of times i just want a default value if the optional value doesn't exist.
+1 -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On 11/28/2011 6:05 PM, Dave Abrahams wrote:
on Fri Nov 18 2011, paul Fultz<pfultz2-AT-yahoo.com> wrote:
I provide a list of modifications that I could think of. What is your opinion? This is more of an addition to your list, but I think adding
the | operator for getting the optional value or a default value would be nice. For example:
optional<double> safe_sqrt(double x) { if (x< 0) return none; else return sqrt(x); }
double x = safe_sqrt(4) | 0; //x = 4 double y = safe_sqrt(-1) | 0; //y = 0
I dont know if this is possible for tr2, but would be really nice addition in the future. Especially, since a lot of times i just want a default value if the optional value doesn't exist. +1 +sqrt(4)

Matt Chambers wrote:
On 11/28/2011 6:05 PM, Dave Abrahams wrote:
on Fri Nov 18 2011, paul Fultz<pfultz2-AT-yahoo.com> wrote:
I provide a list of modifications that I could think of. What is your opinion? This is more of an addition to your list, but I think adding
the | operator for getting the optional value or a default value would be nice. For example:
optional<double> safe_sqrt(double x) { if (x< 0) return none; else return sqrt(x); }
double x = safe_sqrt(4) | 0; //x = 4 double y = safe_sqrt(-1) | 0; //y = 0
I dont know if this is possible for tr2, but would be really nice addition in the future. Especially, since a lot of times i just want a default value if the optional value doesn't exist. +1 +sqrt(4)
-1 since we're all chiming in - I hate ideas like this. Silently giving a seemingly valid answer to an invalid question just hides a bug and makes it harder to find. It's a big source of problems with much component type software. Robert Ramey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Robert Ramey wrote:
Matt Chambers wrote:
On 11/28/2011 6:05 PM, Dave Abrahams wrote:
on Fri Nov 18 2011, paul Fultz<pfultz2-AT-yahoo.com> wrote:
I provide a list of modifications that I could think of. What is your opinion? This is more of an addition to your list, but I think adding
the | operator for getting the optional value or a default value would be nice. For example:
optional<double> safe_sqrt(double x) { if (x< 0) return none; else return sqrt(x); }
double x = safe_sqrt(4) | 0; //x = 4 double y = safe_sqrt(-1) | 0; //y = 0
I dont know if this is possible for tr2, but would be really nice addition in the future. Especially, since a lot of times i just want a default value if the optional value doesn't exist. +1 +sqrt(4)
-1
since we're all chiming in - I hate ideas like this. Silently giving a seemingly valid answer to an invalid question just hides a bug and makes it harder to find. It's a big source of problems with much component type software.
Hmmm - have just read this back, I guessing I'm objecting more to the example than I am to the suggestion itself. I really havn't thought enought about the suggestion itself to have a strong opinion. Robert Ramey
Robert Ramey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (7)
-
Andrey Semashev
-
Andrzej Krzemienski
-
Dave Abrahams
-
Matt Chambers
-
paul Fultz
-
Robert Ramey
-
Vicente J. Botet Escriba