[[maybe_unused]]关于成员变量,GCC警告(错误?)该属性被忽略 [英] [[maybe_unused]] on member variable, GCC warns (incorrectly?) that attribute is ignored

查看:69
本文介绍了[[maybe_unused]]关于成员变量,GCC警告(错误?)该属性被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中:

struct Foo {
    [[maybe_unused]] int member = 1;
    void bar() {
        [[maybe_unused]] int local = 0;
    }
};

int main(int argc, char* argv[]) {
    Foo f{};
    f.bar();
    return 0;
}

GCC发出警告,而Clang和MSVC没有:

GCC emits a warning where Clang and MSVC do not:

warning: 'maybe_unused' attribute ignored [-Wattributes]
     [[maybe_unused]] int member = 1;

据我所知,这应该是合法的(编译器不会忽略).根据标准:

As far as I can tell, this should be legal (and not ignored by the compiler). According to the standard:

10.6.7也许未使用的属性[dcl.attr.unused]
...
2.该属性可以应用于类的声明,typedef名称,变量,非静态数据成员,函数,枚举或枚举器.
...

10.6.7 Maybe unused attribute [dcl.attr.unused]
...
2. The attribute may be applied to the declaration of a class, a typedef-name, a variable, a non-static data member, a function, an enumeration, or an enumerator.
...

我不喜欢挥动编译器错误"锤子,但是我不确定在这种情况下还会发生什么.

I hate to swing around the "compiler bug" hammer, but I'm not sure what else it could be in this case.

有人有什么见识吗?

推荐答案

任何原因都可以出于任何原因被编译器忽略",除非标准另有规定(例如,在明确指出的位置使用属性)禁止).

Any attribute can be "ignored by the compiler" for any reason, except where the standard says otherwise (such as using an attribute in a location where it is expressly forbidden).

GCC并不是说你不能在那放一个;这就是说,将一个放在那儿什么也做不了,因为它们可能不会警告可能未使用的成员变量.

GCC isn't saying you can't put one there; it's saying that putting one there won't do anything, because they probably don't warn about maybe-unused member variables.

这篇关于[[maybe_unused]]关于成员变量,GCC警告(错误?)该属性被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆