Issue 175954
Summary [[maybe_unused]] rejected on typedef (valid in C++17)
Labels new issue
Assignees
Reporter vrukesh
    **What code / commands /steps will reproduce the problem?**

Compile the below code for clang C++17 and g++ C++17.

 typedef int [[maybe_unused]] a;
  int main() { return 0; }

**Compiler Explorer link:** https://godbolt.org/z/YxbfP5Yb5

**What is the expected result?**
Compilation should pass.

**What happens instead?**

Compilation fails on clang.
Compilation is pass on gcc.

$ g++ helloworld.cpp -o helloworld -std=c++17
helloworld.cpp:1:13: warning: attribute ignored [-Wattributes]
    1 | typedef int [[maybe_unused]] a;
 |             ^
helloworld.cpp:1:13: note: an attribute that appertains to a type-specifier is ignored

$ /usr/bin/clang++ helloworld.cpp -o helloworld -std=c++17
helloworld.cpp:1:15: error: 'maybe_unused' attribute cannot be applied to types
    1 | typedef int [[maybe_unused]] a;
      | ^
1 error generated.


As per C++17 standard, `[[maybe_unused]]` may appertain to a class, typedef, variable (including structured bindings), non-static data member, function, enumeration, or enumerator, and no diagnostic is required if it ends up unused. Attributes placed in the declaration-specifiers of a typedef apply to the typedef itself, so the usage is valid.

GCC accepts this code (only warns that the attribute is ignored). Per C++17 [dcl.attr.unused]/1, maybe_unused can appertain to a typedef, so this should be accepted.

So, Clang needs to accept or at most warn, matching the standard.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to