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
5
votes
0 answers

Clang enforce [[nodiscard]] by default?

C++17 has the nodiscard attribute to prevent people ignoring a function's return value. It seems overkill to add this to every single function in a code base. Is there a flag to tell Clang to enforce this by default?
user997112
  • 29,025
  • 43
  • 182
  • 361
5
votes
1 answer

What is the rationale behind having [[nodiscard]] types?

What are usecases in which it is beneficial to use a [[nodiscard]] type? On types, [[nodiscard]] emits a warning if the return value of any function returning an instance of that type is omitted; (Citation from p0068r0): If [[nodiscard]] is marked…
mutableVoid
  • 1,284
  • 2
  • 11
  • 29
5
votes
1 answer

Why is the C++ standard library Container function empty() marked as [[nodiscard]]?

It seems like empty() is the only function that has been marked as nodiscard. Other similar functions like size() have not. Why is that?
Fourmet
  • 538
  • 2
  • 12
4
votes
2 answers

Are new C++17 [[nodiscard]] warnings since Visual Studio 15.6.2 compiler update standards-compliant?

A recent Visual Studio Studio upgrade to version 15.6.2 included a Visual C++ compiler update which causes warnings for the following code at the push_back line due to [[nodiscard]]: #include struct [[nodiscard]] S { int i; }; int…
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
3
votes
1 answer

Is [[nodiscard]] any different from [[gnu::warn_unused_result]]?

I had some code that used the GCC extension [[gnu::warn_unused_result]] (a.k.a. __attribute__((__warn_unused_result__))). Now I attempted to use C2x's [[nodiscard]] and I got an incomprehensible error. I'm not sure if the usage of [[nodiscard]] is…
3
votes
1 answer

Can I declare a type [[nodiscard]] with 'using'?

You can declare a class with the [[nodiscard]] attribute. It may be useful when you know from the semantics of this class that whenever it's returned from a function, it must be used for something. I have exactly this case, and it would be very…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
2
votes
3 answers

Explanation of [[nodiscard]] in C++17

In a project using C++20, CLion suggested me to add [[nodiscard]] to my const class method definitions, e.g., class Test { public: [[nodiscard]] int f(int a, int b) const { return a + b; } } The explanation is Adds [[nodiscard]]…
Green绿色
  • 1,620
  • 1
  • 16
  • 43
2
votes
0 answers

python: prevent discarding of the function return value

There's a common error in the code where people write something like: if (id): query.filter(row.id == id) instead of if (id): query = query.filter(row.id == id) The code looks "valid" and it's very hard to spot these by hand. In C++…
salmin
  • 457
  • 3
  • 12
2
votes
1 answer

Does GCC 7.3 omit the [[nodiscard]] attribute for reference returning member functions?

I've got the following code utilizing the [[nodiscard]] attribute of C++17. class SomeClass { public: /** Methods **/ [[nodiscard]] int getValue() { return n; } [[nodiscard]] int &getRef() { return n; } [[nodiscard]] int *getPtr() {…
Caglayan DOKME
  • 948
  • 8
  • 21
2
votes
2 answers

Is it right to use static_cast to ignore iterator as return value?

I have to handle the [[nodiscard]] warning from std::remove; static_cast(std::remove(stringVar.begin(), stringVar.end(), ' ')); I want the correct way to do it. The warning can be stopped by below code: auto temp =…
Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
1
vote
1 answer

Implicit [[nodiscard]] for all const-tagged methods?

I'd like to update my library's API so that the compiler warns whenever the user calls a const-tagged method and then ignores the method's return value. The reasoning is that since a const method has no side effects, the only reason to call it…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1
vote
1 answer

Q: How to define a nondiscard boolean in C++17, preferably using enumeration?

C++17 supports the [[nondiscard]] attributes for functions classes(structures) and enumeration. The following definition compiles (g++7.4.0) without producing a "warning: ‘nondiscard’ attribute directive ignored" - So I assumed the [nondiscard]]…
PolarBear2015
  • 745
  • 6
  • 14
1
vote
0 answers

Why [[nodiscard]] doesn't work with a reference?

It looks like the attribute [[nodiscard]] doesn't work when the function returns a reference. This code gives the expected warning: int a; [[nodiscard]] int get() { return a; } int main() { get(); } However, this code compiles without any…
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
1
vote
1 answer

Why isn't the new operator declared as [[nodiscard]]?

C++17 introduced the new attribute [[nodiscard]] in p0189r1. When a function is decorated with this attribute, the return type must not be discarded. If it is discarded, a warning is emitted. Example: [[nodiscard]] void* allocateMemory(); void…
Brotcrunsher
  • 1,964
  • 10
  • 32