From 01eaf5bef131ff51274ca57f5ade2d10851aa00b Mon Sep 17 00:00:00 2001 From: Jordan Maples Date: Mon, 10 Aug 2020 16:45:47 -0700 Subject: [PATCH 1/2] macro version --- include/gsl/gsl_util | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 89fb2ee..2e79605 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -32,6 +32,12 @@ #endif // _MSC_VER +#if __has_cpp_attribute(nodiscard) >= 201603L +#define GSL_NODISCARD [[nodiscard]] +#else +#define GSL_NODISCARD +#endif + namespace gsl { // @@ -67,13 +73,13 @@ private: // finally() - convenience function to generate a final_action template -final_action finally(const F& f) noexcept +GSL_NODISCARD final_action finally(const F& f) noexcept { return final_action(f); } template -final_action finally(F&& f) noexcept +GSL_NODISCARD final_action finally(F&& f) noexcept { return final_action(std::forward(f)); } @@ -98,16 +104,16 @@ constexpr T narrow(U u) noexcept(false) { constexpr const bool is_different_signedness = (std::is_signed::value != std::is_signed::value); - + const T t = narrow_cast(u); - + if (static_cast(t) != u || (is_different_signedness && ((t < T{}) != (u < U{})))) { throw narrowing_error{}; } - + return t; } From afe824490ebae9188be0e96ecff3dbc73b37285c Mon Sep 17 00:00:00 2001 From: Jordan Maples Date: Wed, 12 Aug 2020 12:13:19 -0700 Subject: [PATCH 2/2] change macro test to use __cplusplus instead of __has_cpp_attribute --- include/gsl/gsl_util | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 2e79605..974655b 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -32,11 +32,11 @@ #endif // _MSC_VER -#if __has_cpp_attribute(nodiscard) >= 201603L +#if defined(__cplusplus) && (__cplusplus >= 201703L) #define GSL_NODISCARD [[nodiscard]] #else #define GSL_NODISCARD -#endif +#endif // defined(__cplusplus) && (__cplusplus >= 201703L) namespace gsl {