From 6eeed739f1acb81c8b69ceae11bd5f350dab3601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johel=20Ernesto=20Guerrero=20Pe=C3=B1a?= Date: Mon, 14 Jan 2019 22:37:37 -0400 Subject: [PATCH] Make narrow constexpr (#698) --- include/gsl/gsl_util | 9 +++++++++ tests/utils_tests.cpp | 4 ++++ 2 files changed, 13 insertions(+) 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 }