C++ Logo

std-discussion

Advanced search

Re: [[maybe_unused]] classes

From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Date: Thu, 20 Apr 2023 00:17:32 +1000
On Wed, Apr 19, 2023 at 5:52 PM Stephan Bergmann via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> > namespace {
> > struct S1 {}; // expected-warning{{unused}}
> > struct [[maybe_unused]] S2 {};
> > }
> > struct S3 {};
> > struct [[maybe_unused]] S4 {};
> > void f() {
> > S3 s3a; // expected-warning{{unused}}
> > [[maybe_unused]] S3 s3b;
> > S4 s4a; // expected-warning{{unused}}
> > [[maybe_unused]] S4 s4b;
> > }
>
> * But only MSVC warns about the unused variable s4a. Both Clang and GCC
> appear to interpret the [[maybe_unused]] on S4 to extend to variables of
> that type. Should that be considered a violation of the recommended
> practice on the part of Clang and GCC?
>

When I designed [[maybe_unused]] it was not my intention that it would
suppress a warning of s4a. The [[maybe_unused]] attribute is intended to
pertain to a name/entity, and not transitively to other name/entities it
has some relationship with.

The first name and entity in this case is S4 and the class type.

The second name and entity in this case is s4a and the local variable.

The second entity is related to the first in that the second entity has the
first entity as its type, but this doesn't mean it gets implicitly marked
[[maybe_unused]].

It could be that it is a good idea that [[maybe_unused]] is "inherited"
across this particular type-variable relationship. I did not consider this
use case when designing it. If you think it is a good idea - then I
encourage you to write a proposal to extend the semantics of the attribute
in this fashion. If adopted, I'm sure the MSVC guys will implement it. If
rejected for semantic reasons, I'm sure GCC/Clang will follow suit too.

Received on 2023-04-19 14:17:46