user1180790
user1180790

Reputation:

Using [[maybe_unused]] attribute on unused class *tors

Question

Should I use [[maybe_unused]] attribute on unused class *tors?

Example

Let's consider the following example:

public: struct keyData{
    public: [[maybe_unused]] keyData(){};
    public: keyData(::std::chrono::steady_clock::time_point timestamp)
        : lastMod(timestamp)
    {};

    protected: ::std::chrono::steady_clock::time_point lastMod = ::std::chrono::steady_clock::now();
};

I want to init multiple keyDatas using cached time_point timestamps. However, I also provide argumentless constructor that inits keyData, setting lastMod to now() as timestamp was not provided.

Should I mark unused public: keyData(){}; with [[maybe_unused]] argument, as in example code, or not?

Research

[[maybe_unused]]​ description of Standard attributes site says (bold is mine):

Appears in the declaration of a class, a typedef­, a variable, a non­static data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.

So if I understood well, *tors are functions, and as functions they can be marked as [[maybe_unused]], can't they?

Upvotes: 3

Views: 1005

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473292

Should I use [[maybe_unused]] attribute on unused class *tors?

The purpose of [[maybe_unused]] is to prevent the emission of warnings for compilers that might warn about the qualified item being unused. Does your compiler warn about unused constructors? If so, then you should use it to suppress that warning.

But since most compilers don't warn about disused functions, it's best not to bother. The attribute would just be taking up space.

Unless you're advertising your library as being -Wunused-member-function clean, it's probably best for all involved that you not suppress this warning. After all, users who want to use that warning actually want the warning to be there. And users who don't use the warning don't need [[maybe_unused]] to be sprinkled around seemingly at random.

Upvotes: 4

Related Questions