Questions tagged [nodiscard]

[[nodiscard]] is C++17 attribute, which was extended in C++20; its purpose is to warn about discarded return value. Tag questions directly related to usage of this attribute and entities marked with this attribute.

Starting in C++17 [[nodiscard]] encourages the compiler to emit a warning when the return value of a function is discarded. It can be applied to a function, or a type.

There's a defect report that makes [[nodiscard]] work with constructors too, and it is incorporated in C++20.

Starting in C++20, nodiscard can provide a message: [[nodiscard("message")]]

To suppress the warning, cast the result to void; see How can I intentionally discard a [[nodiscard]] return value?

36 questions
55
votes
6 answers

How can I intentionally discard a [[nodiscard]] return value?

Say I have [[nodiscard]] int foo () { return 0; } int main () { foo (); } then error: ignoring return value of ‘int foo()’, declared with attribute nodiscard [-Werror=unused-result] but if int x = foo (); then error: unused variable ‘x’…
spraff
  • 32,570
  • 22
  • 121
  • 229
52
votes
2 answers

Why is std::move not [[nodiscard]] in C++20?

I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20),…
bbalchev
  • 827
  • 1
  • 9
  • 19
24
votes
2 answers

Why clang-tidy suggests to add [[nodiscard]] everywhere?

I have a C++ project where clang-tidy is suggesting to add [[nodiscard]] everywhere. Is this a good practice ? The understanding I have is that [[nodiscard]] should be used only when ignoring the return value could be fatal for program. I have an…
13
votes
5 answers

Is it possible to ignore [[nodiscard]] in a special case?

C++17 has a new attribute, [[nodiscard]]. Suppose, that I have a Result struct, which has this attribute: struct [[nodiscard]] Result { }; Now, if I call a function which returns Result, I got a warning if I don't check the returned Result: Result…
geza
  • 28,403
  • 6
  • 61
  • 135
13
votes
1 answer

Ways to specify [[nodiscard]] before C++17

I need the semantics of the [[nodiscard]] attribute in a non-C++17 codebase. I guess there are compiler dependent ways of achieving this before C++17. Does anyone know these? I am interested in the ones for clang,gcc, and MSVC.
gexicide
  • 38,535
  • 21
  • 92
  • 152
11
votes
1 answer

warning C5240: 'nodiscard': attribute is ignored in this syntactic position

Recently version 16.9.5 of Visual Studio 2019 has been released. It apparently introduced new warning: [[nodiscard]] __declspec(dllexport) bool foo(); //ok __declspec(dllexport) [[nodiscard]] bool bar(); // warning C5240: 'nodiscard': attribute is…
Fedor
  • 17,146
  • 13
  • 40
  • 131
11
votes
1 answer

nodiscard attribute in C#

I'm searching for a way to make it illegal for the user of a method to ignore the returned object/value of a method. In C++, this is possible with the [[nodiscard]] attribute. However, I couldn't find a similar attribute in C#. Is there a standard…
stefan
  • 10,215
  • 4
  • 49
  • 90
9
votes
1 answer

Is there any benefit to tagging my class's constructor(s) as [[nodiscard]] when the class itself is tagged as [[nodiscard]]?

Say I have this class: class [[nodiscard]] MyClass { public: MyClass() : _x(x) {} MyClass(int x) : _x(x) {} private: int _x; }; Does adding the [[nodiscard]] tag individually to the class's constructors change anything? Or is that wholly…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
8
votes
2 answers

[[nodiscard]] in std::function return type definition?

I'm wondering if is there a way to have something like this: using CallbackType = std::function<[[nodiscard]]bool(void)>; (I know above code won't be compiled and complains that nodiscard can't be applied to types!) My goal is to enforce the…
Alireza
  • 800
  • 1
  • 7
  • 21
8
votes
3 answers

Why is unique_ptr::release not defined with [[nodiscard]]?

C++17 added [[nodiscard]]. C++20 added the use of [[nodiscard]] on empty methods, e.g. vector::empty() -- maybe, to avoid user confusion with the method clear (i.e. calling empty() accidentally to clear the vector). Why didn't C++20 use this…
Amir Kirsh
  • 12,564
  • 41
  • 74
7
votes
3 answers

Is nodiscard necessary on operators?

Is the [[nodiscard]] attribute necessary on operators? Or is it safe to assume the compiler will emit a warning like it does for most suspiciously discarded things? E.g. an overloaded operator+, should one apply the attribute? What about special…
j__
  • 632
  • 4
  • 18
7
votes
2 answers

Template parameter dependant [[nodiscard]]

I have a functional object which is a wrapper around another function: template class Wrapper { private: FuncT funcToWrap; public: Wrapper(FuncT ftw) : funcToWrap(ftw){}; template
cerkiewny
  • 2,761
  • 18
  • 36
6
votes
3 answers

Why not apply [[nodiscard]] to every constructor?

Since C++20, [[nodiscard]] can be applied to constructors. http://wg21.link/p1771 has the example: struct [[nodiscard]] my_scopeguard { /* ... */ }; struct my_unique { my_unique() = default; // does not acquire…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
6
votes
2 answers

[[nodiscard]] to a function pointer

I want to use a third party functions, which provides its API through a struct full of function pointers. for example: struct S { using p_func1 = int(*)(int, int); p_func1 func1; using p_func2 = int(*)(char*); p_func2 func2; } The…
Curve25519
  • 654
  • 5
  • 17
5
votes
0 answers

Exclude a function's return value from [[nodiscard]]

I want to mark a class as nodiscard, but exclude the return value of a certain function from the nodiscard requirement. This is my goal: enum class [[nodiscard]] Result { OK1, OK2, ERROR, }; [[ok-to-discard]] // This attribute is made up to…
Haozhun
  • 6,331
  • 3
  • 29
  • 50
1
2 3