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

            Bug ID: 101376
           Summary: Missing
                    Wsuggest-attribute=const/Wsuggest-attribute=pure for
                    throwing functions, wrong Wattributes for pure/const
                    throwing functions
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: harald at gigawatt dot nl
  Target Milestone: ---

According to PR100409, const/pure functions are allowed to throw. As such, I
would expect that

  void f() {
    throw "!";
  }

produces a diagnostic with -Wsuggest-attribute=const but no diagnostic is
produced.

In fact, if I modify this to add the attribute myself, like so:

  __attribute__((const)) void f() {
    throw "!";
  }

I get "warning: 'const' attribute on function returning 'void'". This warning
is documented but now wrong. The documentation reads

  "Because a const function cannot have any observable side effects it does not
make sense for it to return void. Declaring such a function is diagnosed."

A helper function to throw an exception makes sense to declare as returning
void, and meets the criteria of a const function.

Reply via email to