Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::experimental::optional #475

Merged
merged 4 commits into from
Nov 3, 2016
Merged

Conversation

aldanor
Copy link
Member

@aldanor aldanor commented Nov 2, 2016

I'm not sure this would be accepted as PR given it's a TS feature of C++ as of C++14, but we use this internally a lot and find it very useful, so I figured I'd share :)

That being said, it's just a single tiny template in stl.h conditional on C++ standard library feature being present, and it fits pybind11 pretty well, don't see any harm in merging it in.

// optional has been finally accepted into C++ standard earlier this year, so it's not going away and is going to become std::optional in C++17 with the same semantics; std::experimental::optional has been around for quite a while now, e.g. in gcc since 4.9.

@aldanor aldanor force-pushed the feature/optional branch 2 times, most recently from c38a249 to 5b80f49 Compare November 2, 2016 10:39
@jagerman
Copy link
Member

jagerman commented Nov 3, 2016

Perhaps also let it support std::optional (instead std::experimental::optional) if available, so that it won't need change under a C++17 compiler without std::experimental::optional?

In fact, the example in the __has_include proposal is exactly this optional vs experimental::optional include case.

@wjakob
Copy link
Member

wjakob commented Nov 3, 2016

Looks useful! All agreed about the std::experimental prefix -- it should work out of the box on a C++17 capable compiler without experimental namespaces.

PS: do you also have plans for std::variant?

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

@jagerman's suggestion looks good, will add that. It's going to stay untested for a while though :)

std::variant doesn't have a ref implementation yet so probably not at the moment (I've successfully wrapped a few like mpark/variant though).

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

One detail: I guess <experimental/optional> would be still available alongside <optional> in C++17, it wouldn't be nice to just break support for the former if the latter is detected.

Thus, I think the type caster should be defined separately for both (although it would be identical).

@wjakob
Copy link
Member

wjakob commented Nov 3, 2016

Hmm-- if std::experimental::optional and std::optional are both available, why would there be any difference between them? I assume the interface is fully specified, so there is little leeway for differences in behavior.

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

Not sure, ask GCC guys :) But they seem to be actively maintaining both. I guess so as not to break downstream code that uses the experimental version if there are radical changes in the <optional>. Which implies we also have to support both; I'll look if it's possible to impl it without minimal copy/paste.

@wjakob
Copy link
Member

wjakob commented Nov 3, 2016

Aha, I see now. I think this could be done similarly to other parts of stl.h which target multiple containers with an extra layer of template indirection.

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

Ok, this should do it.

template<typename T> struct type_caster<std::optional<T>>
: public optional_caster<std::optional<T>> {};
NAMESPACE_END(detail)
NAMESPACE_END(pybind11)
Copy link
Member

Choose a reason for hiding this comment

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

Very subjective opinion: it may be nice to use the more compact namespace pybind11 { namespace detail { and }} here. Just to improve the signal-to-noise ratio.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, too many shouty macros.

Copy link
Member

Choose a reason for hiding this comment

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

It might be even easier to do all the #includes at the top and make definitions like PYBIND11_HAS_OPTIONAL and PYBIND11_HAS_EXP_OPTIONAL

Then, you could #ifdef those templates without having to open and close namespaces all the time.

Copy link
Member

Choose a reason for hiding this comment

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

Both is fine to me though, whatever you prefer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea that could be cleaner. I also missed one case apparenty (due to using elif instead of if). Fix incoming

@aldanor aldanor force-pushed the feature/optional branch 3 times, most recently from 16b1a19 to 451216b Compare November 3, 2016 11:26
@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

All done, there's now defs in stl.h indicating whether std or std::experimental optionals were found.

@wjakob
Copy link
Member

wjakob commented Nov 3, 2016

Sorry, one more thing: can you add a line to the changelog and add std::optional to the table of supported types in the documentation?

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

Ok sure. We can now even say we support some C++17 features 🐼

@aldanor
Copy link
Member Author

aldanor commented Nov 3, 2016

Added a mention in the docs (unfortunately had to reflow the table), rebased on master.

@wjakob
Copy link
Member

wjakob commented Nov 3, 2016

Great, thank you!

@wjakob wjakob merged commit 44a69f7 into pybind:master Nov 3, 2016
@patrikhuber
Copy link
Contributor

patrikhuber commented Nov 7, 2016

Great work, thank you!

Is there any chance you could make it work with VS15 Preview 5 too? (this is the "next" version of VS, currently in Release Candidate status - not VS2015, which doesn't have <optional> yet).

The coming "VS15" has <optional>, but unfortunately not yet __has_include.

@wjakob
Copy link
Member

wjakob commented Nov 7, 2016

If this is important to you, I suggest that you create a PR with a suitable VS2015-compatible detection method.

@patrikhuber
Copy link
Contributor

I went back to VS2015 for now. I will revisit this once the final version of the next VS is out. (should be quite soon :-) )

@rwgk rwgk mentioned this pull request Feb 9, 2023
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.

None yet

5 participants