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

180 views Asked by At

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 redundant with the [[nodiscard]] tag applied to the class declaration itself, and therefore only adds unnecessary noise to the header file?

(My testing suggests the later, but I might be missing some nuance)

1

There are 1 answers

0
Remy Lebeau On BEST ANSWER

Marking the individual constructors as [[nodiscard]] is completely redundant and unnecessary if the class as a whole is marked as [[nodiscard]].