From d947c740eebd4983203b4ac7a352932f140a6ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 8 Apr 2016 13:16:15 +0200 Subject: [PATCH] Mark finally with nodiscard attribute. The compiler should warn about unsued result of finally(). See https://github.com/Microsoft/GSL/issues/284. --- include/gsl_util.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/gsl_util.h b/include/gsl_util.h index edfcce4..430a57b 100644 --- a/include/gsl_util.h +++ b/include/gsl_util.h @@ -78,13 +78,20 @@ private: bool invoke_; }; +// C++17 [[nodiscard]] attribute for legacy compilers. +#if _MSC_VER +#define GSL_NODISCARD _Check_return_ +#else +#define GSL_NODISCARD __attribute__((warn_unused_result)) +#endif + // finally() - convenience function to generate a final_act template -inline final_act finally(const F &f) +GSL_NODISCARD inline final_act finally(const F &f) noexcept { return final_act(f); } template -inline final_act finally(F &&f) noexcept +GSL_NODISCARD inline final_act finally(F &&f) noexcept { return final_act(std::forward(f)); } // narrow_cast(): a searchable way to do narrowing casts of values