mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Merge pull request #908 from JordanMaples/fix_nodiscard_in_finally_macro
Finally [[nodiscard]] - Version 2
This commit is contained in:
commit
0c80f51f7c
@ -32,6 +32,12 @@
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
#define GSL_NODISCARD [[nodiscard]]
|
||||
#else
|
||||
#define GSL_NODISCARD
|
||||
#endif // defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
//
|
||||
@ -67,13 +73,13 @@ private:
|
||||
|
||||
// finally() - convenience function to generate a final_action
|
||||
template <class F>
|
||||
final_action<F> finally(const F& f) noexcept
|
||||
GSL_NODISCARD final_action<F> finally(const F& f) noexcept
|
||||
{
|
||||
return final_action<F>(f);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
final_action<F> finally(F&& f) noexcept
|
||||
GSL_NODISCARD final_action<F> finally(F&& f) noexcept
|
||||
{
|
||||
return final_action<F>(std::forward<F>(f));
|
||||
}
|
||||
@ -98,16 +104,16 @@ constexpr
|
||||
T narrow(U u) noexcept(false)
|
||||
{
|
||||
constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value);
|
||||
|
||||
|
||||
const T t = narrow_cast<T>(u);
|
||||
|
||||
|
||||
if (static_cast<U>(t) != u
|
||||
|| (is_different_signedness
|
||||
&& ((t < T{}) != (u < U{}))))
|
||||
{
|
||||
throw narrowing_error{};
|
||||
}
|
||||
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user