[Bug c++/80311] New: Bound member functions extension allows bogus conversions

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Apr 4 14:07:00 GMT 2017


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

            Bug ID: 80311
           Summary: Bound member functions extension allows bogus
                    conversions
           Product: gcc
           Version: 7.0.1
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

struct X {
  int i = 0;
  int f() { return i; }
};

int main() {
  X x;
  using fp = int (*)();
  auto f = (fp)(&X::f);
  return f();
}

This bogus attempt to use
https://gcc.gnu.org/onlinedocs/gcc/Bound-member-functions.html fails, because
there is no 'this' pointer provided for the call to the bound function.

G++ should notice this and give a diagnostic, even with -Wno-pmf-conversions.

The diagnostic should be given when converting to a function pointer that
doesn't match the arguments that would be needed for the bound function.

i.e. for (fp)(&X::f) or (fp)(x.*pmf) where that resovles to a pointer to:

  R Y::f(Args...)

we should warn if fp is not R (*)(Y*, Args...).

This should simply be an error, not a warning. I see no reason to allow the
extension to form invalid function pointers. Code that really really wants to
do so can always perform a conversion after forming the bound function pointer:

struct X {
  int i = 0;
  int f() { return i; }
};

int main() {
  X x;
  using wrong_type = int(*)();
  using right_type = int(*)(X*);
  auto f = (wrong_type)(right_type)(&X::f);
  return ((right_type)f)();
}

Maybe -fpermissive could cause it to be allowed without the extra conversion,
for legacy code misusing the feature.


More information about the Gcc-bugs mailing list