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

513 Views Asked by At

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 convenient to tag the class with [[nodiscard]] instead of each individual function that returns it. However, there's one complication. This is the type that I need to make [[nodiscard]]:

using ConnectionStruct = std::pair<std::shared_ptr<CMutex>, std::shared_ptr<SignalFunction>>;

Is there a syntax to mark my ConnectionStruct as [[nodiscard]]?

1

There are 1 best solutions below

0
On BEST ANSWER

[[nodiscard]] cannot be applied to aliases. Only to:

The attribute-token nodiscard may be applied to the declarator-id in a function declaration or to the declaration of a class or enumeration.

Emphasis added.