diff --git a/doc/macro_reference.qbk b/doc/macro_reference.qbk index b67b7c87..f39bb704 100644 --- a/doc/macro_reference.qbk +++ b/doc/macro_reference.qbk @@ -1275,6 +1275,8 @@ Usage example: ]] [[`BOOST_ATTRIBUTE_UNUSED`][Expands to `__attribute__((unused))` when this is available - can be used to disable compiler warnings relating to unused types or variables.]] +[[`BOOST_ATTRIBUTE_NODISCARD`][Expands to `[[nodiscard]]` when this is available - +can be used to create a warning when a type or variable is unused.]] [[`BOOST_MAY_ALIAS`, `BOOST_NO_MAY_ALIAS`][ `BOOST_MAY_ALIAS` expands to a type attribute that can be used to mark types that may alias other types. Pointers or references to such marked types can be used to access objects diff --git a/include/boost/config/detail/suffix.hpp b/include/boost/config/detail/suffix.hpp index 6218f184..465d5e47 100644 --- a/include/boost/config/detail/suffix.hpp +++ b/include/boost/config/detail/suffix.hpp @@ -992,6 +992,17 @@ namespace std{ using ::type_info; } #ifndef BOOST_ATTRIBUTE_UNUSED # define BOOST_ATTRIBUTE_UNUSED #endif +// +// [[nodiscard]]: +// +#ifdef __has_cpp_attribute +#if __has_cpp_attribute(nodiscard) +# define BOOST_ATTRIBUTE_NODISCARD [[nodiscard]] +#endif +#endif +#ifndef BOOST_ATTRIBUTE_NODISCARD +# define BOOST_ATTRIBUTE_NODISCARD +#endif #define BOOST_STATIC_CONSTEXPR static BOOST_CONSTEXPR_OR_CONST diff --git a/test/helper_macro_test.cpp b/test/helper_macro_test.cpp index 2f707dc6..3a3b4f01 100644 --- a/test/helper_macro_test.cpp +++ b/test/helper_macro_test.cpp @@ -37,6 +37,8 @@ BOOST_NORETURN void always_throw() struct BOOST_MAY_ALIAS aliasing_struct {}; typedef unsigned int BOOST_MAY_ALIAS aliasing_uint; +struct BOOST_ATTRIBUTE_NODISCARD nodiscard_struct {}; + #define test_fallthrough(x) foobar(x) @@ -53,6 +55,7 @@ int main() result += never_inline(3); if(BOOST_UNLIKELY(!result)) always_throw(); + nodiscard_struct s; } catch(int) {