Skip to content

Adds forwarding make_optional helpers, resolves #30#31

Merged
akrzemi1 merged 1 commit intoboostorg:developfrom
daniel-j-h:forwarding-make-optional
May 17, 2017
Merged

Adds forwarding make_optional helpers, resolves #30#31
akrzemi1 merged 1 commit intoboostorg:developfrom
daniel-j-h:forwarding-make-optional

Conversation

@daniel-j-h
Copy link
Contributor

For #30. The varargs overloads probably make no sense with the bool argument at the second position?

I don't quite understand why the test/optional_test_fail_copying_a_moveable_type.cpp fails to compile here (unrelated to this changeset) - shouldn't construction from a moveable-only type work?

Also how can I provide docs for the overload depending on if rvalue refs are enabled or not? I found the quickbook files in doc I'm just wondering what the best way to document the overloads is.

cc @akrzemi1

@akrzemi1
Copy link
Member

The code you highlighted triggers uses val as an lvalue: therefore it chooses to copy rather than move.
For the docs and unit tests, you do not have to bother about these. When I get to merge your change (be patient with me), I will update the docs also. My solution in this case is to only document for the case with rvalue references enabled. For the rest I only say: "if rvalues are disabled, the library degrades in a friendly way".

@akrzemi1 akrzemi1 merged commit 6cd5135 into boostorg:develop May 17, 2017
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants