diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 542bbaa..1558044 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -37,6 +37,12 @@ #endif // _MSC_VER < 1910 #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 { // @@ -111,6 +117,9 @@ namespace details template GSL_SUPPRESS(type.1) // NO-FORMAT: attribute 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 t = narrow_cast(u); diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 05f8d0f..5cacd97 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -126,4 +126,8 @@ TEST_CASE("narrow") n = -42; CHECK_THROWS_AS(narrow(n), narrowing_error); + +#if GSL_CONSTEXPR_NARROW + static_assert(narrow(120) == 120, "Fix GSL_CONSTEXPR_NARROW"); +#endif }