From ca0e82b79f7115758b25f2ff275305329c85d05f Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 9 May 2017 14:34:45 -0400 Subject: [PATCH] Update PYBIND11_CPP1{4,7} macros for MSVC The PYBIND11_CPP14 macro started out as a guard for the compile-time path code in `descr.h`, but has since come to mean other things. This means that while the `descr.h` check has just checked the `PYBIND11_CPP14` macro, various other places now check `PYBIND11_CPP14 || _MSC_VER`. This reverses that by now setting the CPP14 macro when MSVC is trying to support C++14, but disabling the `descr.h` C++14 code (which still fails under MSVC 2017). The CPP17 macro also gets enabled when MSVC 2017 is compiling with /std:c++latest (the default is /std:c++14), which enables `std::optional` and `std::variant` support under MSVC. --- include/pybind11/common.h | 21 +++++++++++++-------- include/pybind11/descr.h | 4 +++- include/pybind11/pybind11.h | 2 +- include/pybind11/stl.h | 5 +++++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 58fc205e1..7c53638ca 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -16,9 +16,6 @@ # define NAMESPACE_END(name) } #endif -// Neither MSVC nor Intel support enough of C++14 yet (in particular, as of MSVC 2015 and ICC 17 -// beta, neither support extended constexpr, which we rely on in descr.h), so don't enable pybind -// CPP14 features for them. #if !defined(_MSC_VER) && !defined(__INTEL_COMPILER) # if __cplusplus >= 201402L # define PYBIND11_CPP14 @@ -26,6 +23,14 @@ # define PYBIND11_CPP17 # endif # endif +#elif defined(_MSC_VER) +// MSVC sets _MSVC_LANG rather than __cplusplus (supposedly until the standard is fully implemented) +# if _MSVC_LANG >= 201402L +# define PYBIND11_CPP14 +# if _MSVC_LANG > 201402L && _MSC_VER >= 1910 +# define PYBIND11_CPP17 +# endif +# endif #endif // Compiler version assertions @@ -69,7 +74,7 @@ # define PYBIND11_NOINLINE __attribute__ ((noinline)) #endif -#if defined(PYBIND11_CPP14) || defined(_MSC_VER) +#if defined(PYBIND11_CPP14) # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]] #else # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) @@ -339,7 +344,7 @@ struct internals { inline internals &get_internals(); /// from __cpp_future__ import (convenient aliases from C++14/17) -#ifdef PYBIND11_CPP14 +#if defined(PYBIND11_CPP14) && (!defined(_MSC_VER) || _MSC_VER >= 1910) using std::enable_if_t; using std::conditional_t; using std::remove_cv_t; @@ -350,7 +355,7 @@ template using remove_cv_t = typename std::remove_cv::type; #endif /// Index sequences -#if defined(PYBIND11_CPP14) || defined(_MSC_VER) +#if defined(PYBIND11_CPP14) using std::index_sequence; using std::make_index_sequence; #else @@ -622,8 +627,8 @@ struct error_scope { /// Dummy destructor wrapper that can be used to expose classes with a private destructor struct nodelete { template void operator()(T*) { } }; -// overload_cast requires variable templates: C++14 or MSVC -#if defined(PYBIND11_CPP14) || defined(_MSC_VER) +// overload_cast requires variable templates: C++14 +#if defined(PYBIND11_CPP14) #define PYBIND11_OVERLOAD_CAST 1 NAMESPACE_BEGIN(detail) diff --git a/include/pybind11/descr.h b/include/pybind11/descr.h index 2c3fb3d13..65476606b 100644 --- a/include/pybind11/descr.h +++ b/include/pybind11/descr.h @@ -15,7 +15,9 @@ NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(detail) -#if defined(PYBIND11_CPP14) /* Concatenate type signatures at compile time using C++14 */ +/* Concatenate type signatures at compile time using C++14 */ +#if defined(PYBIND11_CPP14) && !defined(_MSC_VER) +#define PYBIND11_CONSTEXPR_DESCR template class descr { template friend class descr; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index f00c0f9f9..098d7e0cf 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -252,7 +252,7 @@ protected: if (type_depth != 0 || types[type_index] != nullptr) pybind11_fail("Internal error while parsing type signature (2)"); - #if !defined(PYBIND11_CPP14) + #if !defined(PYBIND11_CONSTEXPR_DESCR) delete[] types; delete[] text; #endif diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 329ff645a..8ca7fd37d 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -41,6 +41,11 @@ # include # define PYBIND11_HAS_VARIANT 1 # endif +#elif defined(_MSC_VER) && defined(PYBIND11_CPP17) +# include +# include +# define PYBIND11_HAS_OPTIONAL 1 +# define PYBIND11_HAS_VARIANT 1 #endif NAMESPACE_BEGIN(pybind11)