[PATCH] D45179: [libc++] Add _LIBCPP_FORCE_NODISCARD define to force-enable nodiscard in pre-C++17

Arthur O'Dwyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 11 11:15:29 PDT 2018


Quuxplusone added a comment.

In https://reviews.llvm.org/D45179#1064589, @lebedev.ri wrote:

> I'm waiting for @mclow.lists to have the final say re this differential.


Ack. :)

> So roughly:
> 
>   // NOTE: Do not use [[nodiscard]] in pre-C++17 mode
>   //       to avoid -Wc++17-extensions warning.
>   // And we can't use GCC's [[gnu::warn_unused_result]] and
>   // __attribute__((warn_unused_result)),
>   // because GCC does not silence them via (void) cast.
>   #if !defined(_LIBCPP_NODISCARD)
>   #  if __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17
>   #    define _LIBCPP_NODISCARD [[nodiscard]]
>   #  elif __has_cpp_attribute(clang::warn_unused_result)
>   #    define _LIBCPP_NODISCARD [[clang::warn_unused_result]]
>   #  else
>   #    define _LIBCPP_NODISCARD
>   #  endif
>   #endif
> 
> 
> ?

Yes, that correctly expresses my intent. Personally I would write it like this to reduce the nesting and linecount:

  // NOTE: Do not use [[nodiscard]] in pre-C++17 mode
  //       to avoid -Wc++17-extensions warning.
  // And we can't use GCC's [[gnu::warn_unused_result]] and
  // __attribute__((warn_unused_result)),
  // because GCC does not silence them via (void) cast.
  #if defined(_LIBCPP_NODISCARD)
  #elif __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17
  #  define _LIBCPP_NODISCARD [[nodiscard]]
  #elif __has_cpp_attribute(clang::warn_unused_result)
  #  define _LIBCPP_NODISCARD [[clang::warn_unused_result]]
  #else
  #  define _LIBCPP_NODISCARD
  #endif


Repository:
  rCXX libc++

https://reviews.llvm.org/D45179





More information about the cfe-commits mailing list