Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/boost/optional/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,25 @@ class optional<T&&>

namespace boost {

#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES

template<class T>
inline
optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
{
return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(boost::forward<T>(v));
}

// Returns optional<T>(cond,v)
template<class T>
inline
optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
{
return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,boost::forward<T>(v));
}

#else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it intended to remove the l-value methods with this #else?
I'm getting a compile error trying to make an optional from a variable, e.g. float x = 1; make_optional<float>(x);
(without -DBOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function make_optional is not intended to be used like this, with template arguments specified explicitly. The only purpose of function make_optional is to spare you the trouble of specifying the type of optional value. If you want to specify it yourself, you have a simpler version:

float x = 1; boost::optional<float>(x);

Macro BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES is not intended to be defined by users. The library defines it when it detects old compilers without rvalue reference support.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed some detail… I was attempting to use boost::make_optional<T>(bool, T) to simplify the following:

float value;
bool valid = GetValidAndValue(&value);

// return valid ? boost::optional<float>(value) : boost::none;
return boost::make_optional<float>(valid, value);

True it is not a huge simplification - but should that be possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, same response. If you can afford spelling out the type of optional value use:

return boost::optional<float>(valid, value);

If you want to the type of optional value to be deduced use:

return boost::make_optional(valid, value);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IOW, make_optional is only used for type deduction.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, got it... thanks!


// Returns optional<T>(v)
template<class T>
inline
Expand All @@ -1292,6 +1311,8 @@ optional<T> make_optional ( bool cond, T const& v )
return optional<T>(cond,v);
}

#endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES

// Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
// No-throw
template<class T>
Expand Down