Skip to content

Use custom definitions for negation and bool_constant #737

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

Merged
merged 3 commits into from
Mar 17, 2017

Conversation

jcelerier
Copy link
Contributor

Instead of relying on sometimes missing C++17 definitions

As requested per PR#735

Instead of relying on sometimes missing C++17 definitions
#if defined(PYBIND11_CPP17) || defined(_MSC_VER)
using std::bool_constant;
using std::negation;
#else
template <bool B> using bool_constant = std::integral_constant<bool, B>;
template <class T> using negation = bool_constant<!T::value>;
Copy link
Member

@dean0x7d dean0x7d Mar 16, 2017

Choose a reason for hiding this comment

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

Replace this line with the following to fix the MSVC internal compiler error:

template <typename T> struct negation : bool_constant<!T::value> { };

The other compilers can get away with just the alias, but I don't think it's worth splitting up the implementation over it.

@jagerman
Copy link
Member

In general, I'm a little hesitant to bother worrying about significantly incomplete C++17 support in compilers (or the associated stl, in this case); we're talking about experimental support for a not-yet-released C++ standard.

In this case, the workaround is pretty simple (and actually slightly reduces code), so it seems acceptable; but there's a reason we do the C++17 travis-ci testing on a gcc/stdlibc++ 7 snapshot and clang/libc++ 4.0 release: those are the first to support a reasonable set of C++17 features. (I believe AppleClang/libc++ is based on LLVM 3.9).

So I guess what I'm saying is: "doesn't work in unreleased standard mode on a non-cutting edge compiler" really doesn't seem an appropriate PR justification for anything more than a trivial change.

@dean0x7d
Copy link
Member

@jagerman I agree completely.

@jcelerier
Copy link
Contributor Author

jcelerier commented Mar 16, 2017

In general, I'm a little hesitant to bother worrying about significantly incomplete C++17 support in compilers

I still think that the right way for this is to rely on feature-test macros (here's a list) : it does not add much code and you can be certain that what you want to use is there.

@jagerman
Copy link
Member

Unfortunately MSVC doesn't support them, so it would still need the MSVC version checks, but yes, I agree that that's a better approach.

Regarding this PR: part of the reason I like the using std::negation and similarly for bool_constant isn't that it buys us anything in terms of efficiency, but rather that it makes our intention instantly clear: what we want is C++17's std::negation, but if that isn't available, we provide our own implementation. (And similarly for C++14 features like enable_if_t). With this PR, yes, we're still doing that, but it's slightly less obvious that our intention here is to backport a C++17 feature.

@jagerman
Copy link
Member

As such I think I'd prefer this PR if, instead of removing the "using", it replaced the condition with

#if defined(__cpp_lib_logical_traits) || defined(_MSC_VER)

(Then you also don't need the MSVC struct fix).

@jagerman
Copy link
Member

(Or you could accomplish the same with a simple comment to that effect; I'll let others weigh in).

@dean0x7d
Copy link
Member

I thought removing the condition would simplify things since the std implementation really doesn't buy us anything in this case. But ultimately I don't have strong feelings either way and I'll defer to you and @jcelerier.

I will say the SD-6 feature-test macros look great and I wasn't aware that they were semi-standardized. I'm definitely in favor of using them for C++17 features going forward since it looks like GCC, clang and Intel/EDG support them.

@dean0x7d dean0x7d merged commit 68e089a into pybind:master Mar 17, 2017
@dean0x7d
Copy link
Member

Merged, thanks @jcelerier!

@jcelerier
Copy link
Contributor Author

thanks to you ! we can finally get back to work :p

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