https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88738
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Ulrich Drepper from comment #3) > Created attachment 45416 [details] > Add nodiscard support > > As Martin suggested, we could indeed use existing attributes in library code > to warn about some of the problems. The code from comment #0 is real, this > happened in a project of mine where I mistyped an assignment. The warning > would have pointed to the problem. > > How about the following patch for a start? This compiles cleanly on x86-64. > I haven't run the test suite to see whether it breaks some regression tests. Please do, but it looks good in principle. I'd feel comfortable adding this in stage 4 if it was only enabled for C++17 and up: // Macro to warn about unused results. #if __cplusplus >= 201703L # define _GLIBCXX_NODISCARD [[__nodiscard__]] #else # define _GLIBCXX_NODISCARD #endif We could then enable it for C++98/C++11/C++14 during stage 1 if no problems show up. > Also, this approach should be extended beyond shared_ptr and unique_ptr, > probably to at least every single bool operatorXX(...) const. Or even every > single const member function which then of course raises the question > whether the compiler should learn about this… Microsoft went quite aggressive adding [[nodiscard]] *everywhere* in their library, and ended up backing some out again. There are some subtle cases (or, depending on your point of view, users do silly things and complain about getting warnings for their unreasonable code). I do think Clang's warning in the compiler is better than having to decorate everything explicitly.