gpt4 book ai didi

c++ - 在未使用的类 *tors 上使用 [[maybe_unused]] 属性

转载 作者:太空狗 更新时间:2023-10-29 20:51:58 28 4
gpt4 key购买 nike

问题

我应该使用 [[maybe_unused]] attribute在未使用的类 *tors 上?

示例

让我们考虑以下示例:

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();
};

我想使用缓存 time_point 初始化多个 keyData时间戳。但是,我还提供了初始化 keyData 的无参数构造函数,将 lastMod 设置为 now()因为没有提供时间戳。

我是否应该像示例代码那样使用 [[maybe_unused]] 参数标记未使用的 public: keyData(){};

研究

[[maybe_unused]] Standard attributes 的描述网站说(粗体是我的):

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.

  • Constructors and member initializerlists网站说(粗体是我的):

    Constructor is a special non-static member function of a class that is used to initialize objects of its class type.

  • Destructors网站说(粗体是我的):

    A destructor is a special member function that is called when the lifetime of an object ends.

  • operator overloading网站说(粗体是我的):

    Overloaded operators are functions with special function names.

所以如果我理解得很好,*tors 是函数,作为函数,它们可以标记为 [[maybe_unused]],不是吗?

最佳答案

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

[[maybe_unused]] 的目的是防止向编译器发出可能警告合格项目未使用的警告。您的编译器是否警告未使用的构造函数?如果是这样,那么您应该使用它来抑制该警告。

但是由于大多数编译器不会对废弃的函数发出警告,所以最好不要打扰。该属性只会占用空间。

除非您宣传您的库是 -Wunused-member-function 干净的,否则最好不要抑制此警告,这对所有相关人员来说都是最好的。毕竟,想要使用该警告的用户实际上希望警告就在那里。不使用警告的用户不需要 [[maybe_unused]] 似乎是随机散布的。

关于c++ - 在未使用的类 *tors 上使用 [[maybe_unused]] 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47582660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com