[Bug c++/90894] New: maybe_unused attribute is ignored on function parameters in explicitly instantiated templates

gcc at mattwhitlock dot name gcc-bugzilla@gcc.gnu.org
Mon Jun 17 07:07:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90894

            Bug ID: 90894
           Summary: maybe_unused attribute is ignored on function
                    parameters in explicitly instantiated templates
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at mattwhitlock dot name
  Target Milestone: ---

/* BEGIN bug.cpp */

template <int X>
struct Base {
        void func(int);
};

struct Derived : Base<0> { };

template <int X>
void Base<X>::func([[maybe_unused]] int n) { }  // WARNING

template class Base<0>;

/*-------------*/

template <int X>
void func(int);

template void func<0>(int);

template <int X>
void func([[maybe_unused]] int n) { }  // WARNING

/* END bug.cpp */


$ g++ -c -Wunused-parameter bug.cpp
bug.cpp: In instantiation of 'void Base<X>::func(int) [with int X = 0]':
bug.cpp:11:16:   required from here
bug.cpp:9:6: warning: 'maybe_unused' attribute directive ignored [-Wattributes]
    9 | void Base<X>::func([[maybe_unused]] int n) { }  // WARNING
      |      ^~~~~~~
bug.cpp:3:12: warning: unused parameter 'n' [-Wunused-parameter]
    3 |  void func(int);
      |            ^~~
bug.cpp: In instantiation of 'void func(int) [with int X = 0]':
bug.cpp:18:26:   required from here
bug.cpp:21:6: warning: 'maybe_unused' attribute directive ignored
[-Wattributes]
   21 | void func([[maybe_unused]] int n) { }  // WARNING
      |      ^~~~
bug.cpp:16:11: warning: unused parameter 'n' [-Wunused-parameter]
   16 | void func(int);
      |           ^~~


The first part of the test case (above the dashed line) demonstrates the
problem on a class template. Note that the warning disappears if the Derived
class definition is removed or is moved below the class template member
function definition.

The second part of the test case (below the dashed line) demonstrates the
problem on a function template. Note that the warning disappears if the
explicit function template instantiation is moved below the function template
definition.


More information about the Gcc-bugs mailing list