[Bug c++/105703] New: Add fix-it for missing nested-name-specifier on non-member function using 'this'

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 23 11:10:15 GMT 2022


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

            Bug ID: 105703
           Summary: Add fix-it for missing nested-name-specifier on
                    non-member function using 'this'
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

This is a generalization of the suggestion in PR 105702.

Given:

struct A {
  void f();
};

void f() { (void) *this; }


We correctly diagnose the use of 'this'

ool.C:5:20: error: invalid use of 'this' outside of a non-static member
function
void f() { (void) *this; }
                   ^


However, the use of 'this' is not the problem. The user intended to define
A::f() here but missed the A:: qualification (maybe by moving an in-class
definition to be out-of-class, and forgetting to add the A:: to it).

If we give this error for use of 'this' in a non-member function with signature
R(Args...), we should look to see if any class previously defined has a
non-defining declaration for a member function with that same signature. If
yes, assume the non-member function was meant to be qualified with the class
name.

So we would print:

ool.C:5:20: error: invalid use of 'this' outside of a non-static member
function
void f() { (void) *this; }
                   ^
ool.C:5:6: note: did you mean to define a member of A?
   5 | void f() { (void) *this; }
     |      ^
     |      A::


More information about the Gcc-bugs mailing list