Reputation:
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 keyData
s 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 nonstatic 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.
Constructors and member initializer lists site says (bold is mine):
Constructor is a special non-static member function of a class that is used to initialize objects of its class type.
Destructors site says (bold is mine):
A destructor is a special member function that is called when the lifetime of an object ends.
operator overloading site says (bold is mine):
Overloaded operators are functions with special function names.
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
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