Make narrow constexpr (#698)

This commit is contained in:
Johel Ernesto Guerrero Peña 2019-01-14 22:37:37 -04:00 committed by Anna Gringauze
parent 9ff6e19ea9
commit 6eeed739f1
2 changed files with 13 additions and 0 deletions

View File

@ -37,6 +37,12 @@
#endif // _MSC_VER < 1910 #endif // _MSC_VER < 1910
#endif // _MSC_VER #endif // _MSC_VER
#if (defined(_MSC_VER) && _MSC_VER < 1910) || (!defined(__clang__) && defined(__GNUC__) && __GUNC__ < 6)
#define GSL_CONSTEXPR_NARROW 0
#else
#define GSL_CONSTEXPR_NARROW 1
#endif
namespace gsl namespace gsl
{ {
// //
@ -111,6 +117,9 @@ namespace details
template <class T, class U> template <class T, class U>
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false) GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
#if GSL_CONSTEXPR_NARROW
constexpr
#endif
T narrow(U u) noexcept(false) T narrow(U u) noexcept(false)
{ {
T t = narrow_cast<T>(u); T t = narrow_cast<T>(u);

View File

@ -126,4 +126,8 @@ TEST_CASE("narrow")
n = -42; n = -42;
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error); CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
#if GSL_CONSTEXPR_NARROW
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
#endif
} }