c++c++17nodiscardc++-attributes

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 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)


Solution

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