Use std::byte when available. (#519)

* Use std::byte implementation when available with MSVC.

* Rollback the /std:c++latest flag used for testing.

* Review feedback.
This commit is contained in:
Neil MacIntosh 2017-05-31 18:42:06 -07:00 committed by GitHub
parent 247c4250d4
commit 7731a91d75

View File

@ -37,10 +37,44 @@
#pragma push_macro("noexcept") #pragma push_macro("noexcept")
#define noexcept /*noexcept*/ #define noexcept /*noexcept*/
#endif // _MSC_VER <= 1800 #endif // _MSC_VER <= 1800
// this tests if we are under MSVC and the standard lib has std::byte and it is enabled
#if _MSC_VER >= 1911 && (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE)
#define GSL_USE_STD_BYTE 1
#else // _MSC_VER >= 1911 && (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE)
#define GSL_USE_STD_BYTE 0
#endif // _MSC_VER >= 1911 && (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE)
#else // _MSC_VER
// this tests if we are under GCC or Clang with enough -std:c++1z power to get us std::byte
#if defined(__cplusplus) && (__cplusplus > 201703L)
#define GSL_USE_STD_BYTE 1
#else // defined(__cplusplus) && (__cplusplus > 201703L)
#define GSL_USE_STD_BYTE 0
#endif //defined(__cplusplus) && (__cplusplus > 201703L)
#endif // _MSC_VER #endif // _MSC_VER
namespace gsl namespace gsl
{ {
#if GSL_USE_STD_BYTE
#include <cstddef>
using std::byte;
using std::to_integer;
#else // GSL_USE_STD_BYTE
// This is a simple definition for now that allows // This is a simple definition for now that allows
// use of byte within span<> to be standards-compliant // use of byte within span<> to be standards-compliant
enum class byte : unsigned char enum class byte : unsigned char
@ -109,6 +143,8 @@ inline constexpr IntegerType to_integer(byte b) noexcept
return static_cast<IntegerType>(b); return static_cast<IntegerType>(b);
} }
#endif // GSL_USE_STD_BYTE
template <bool E, typename T> template <bool E, typename T>
inline constexpr byte to_byte_impl(T t) noexcept inline constexpr byte to_byte_impl(T t) noexcept
{ {