9

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)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • 1
    Note that there is a point to marking a constructor as `nodiscard`. However, this case (an explicit conversion that calls that constructor) is also covered by marking the type `nodiscard`. – Nicol Bolas Apr 24 '23 at 21:40

1 Answers1

5

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770