首页 > 解决方案 > Maybe_unused 属性说明符可以只出现一次并且仍然有效吗?

问题描述

我喜欢这个[[maybe_unused]]属性,并且在我需要编译仅调试功能实现时经常使用它。将成员函数参数指定为[[maybe_unused]].

鉴于:

//Foo.h
class Foo {
    void DebugOnly(Bar& fly);
}

//Foo.cpp
void Foo::DebugOnly(Bar& fly) {
#ifdef _DEBUG
    fly.squish();
#endif
}

在这两个地方指定它会有所不同吗?

//Foo.h
class Foo {
    void DebugOnly([[maybe_unused]] Bar& fly);
}

//Foo.cpp
void Foo::DebugOnly([[maybe_unused]] Bar& fly) {
#ifdef _DEBUG
    fly.squish();
#endif
}

还是我应该只在头文件或实现文件中声明它?

仅标头

//Foo.h
class Foo {
    void DebugOnly([[maybe_unused]] Bar& fly);
}

//Foo.cpp
void Foo::DebugOnly(Bar& fly) {
#ifdef _DEBUG
    fly.squish();
#endif
}

仅实现文件

//Foo.h
class Foo {
    void DebugOnly(Bar& fly);
}

//Foo.cpp
void Foo::DebugOnly([[maybe_unused]] Bar& fly) {
#ifdef _DEBUG
    fly.squish();
#endif
}

cppreference上的页面不区分函数参数,仅将它们归类为“变量”。

标签: c++c++17

解决方案


推荐阅读