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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #4)
> Second, it's unclear to me what purpose the warning is meant to serve in
> this case.  Since a function attribute always applies to the instance of the
> function it decorates and never affects its type the warning doesn't
> indicate anything unusual or unexpected, and only serves to confuse users. 

Actually, most of these attributes, including warn_unused_result, apply to the
function type, not to the function declaration.  That's why you get the
warning.  As an example of this:

__attribute ((warn_unused_result)) int f();

decltype(f)* p;

int main()
{
  p(); // warns about unused result, by design.
}

The warning is letting you know that the attribute is being discarded, so you
don't get the warning within a template:

__attribute ((warn_unused_result)) int f();

template <class T>
struct A {
  T *p;
  void g() { p(); }             // no warning                                   
};

int main()
{
  A<decltype(f)>().g();
}

Note that the C++17 [[nodiscard]] attribute applies to the function, so it
works differently.

> (In cases where the function is declared in a system header it's also
> unclear how the should be avoided.)

If you don't want this warning, -Wno-ignored-attributes will silence it.

Reply via email to