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; }