Ежедневный бит (e) C++ # 113, атрибут C++ 17 [[nodiscard]].
C++17 представил атрибут [[nodiscard]]
, который вызывает предупреждение компилятора, когда результат вызова функции отбрасывается.
Как минимум, этот атрибут следует использовать для функций, выполнение которых требует больших затрат, и функций запроса, которые можно спутать с их аналогами действий.
struct MyStruct { [[nodiscard]] ExpensiveResult expensive_call(); }; struct CustomVector { // C++20 - optional string that will be included in the error [[nodiscard("Did you mean to call clear()?")]] bool empty() const; }; MyStruct x; x.expensive_call(); // warning: ignoring return value of // 'ExpensiveResult MyStruct::expensive_call()', // declared with attribute 'nodiscard' CustomVector y; y.empty(); // warning: ignoring return value of // 'bool CustomVector::empty() const', // declared with attribute 'nodiscard': 'Did you mean to call clear()?'