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

            Bug ID: 85087
           Summary: call to a non-const member function on a const lvalue
                    accepted
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While looking at bug 85043 I noticed that in the test case below, GCC correctly
rejects the attempt to convert the const reference to B to A in the call to g()
but it accepts the same invalid conversion in the context where a a non-const
member function on the result of the conversion is called.  Other compilers
reject both conversions.

$ cat u.C && /ssd/build/gcc-git/gcc/xgcc -B /ssd/build/gcc-git/gcc -S -Wall
-Wextra -Wpedantic u.C
struct A { void f (); };

struct B {
  operator const A& () const;
};

void g (A&);

void h (const B &b)
{
  g ((A)b);      // error: good

  ((A)b).f ();   // accepted: bug
}
u.C: In function ‘void h(const B&)’:
u.C:11:6: error: cannot bind non-const lvalue reference of type ‘A&’ to an
rvalue of type ‘A’
   g ((A)b);      // error: good
      ^~~~
u.C:7:6: note:   initializing argument 1 of ‘void g(A&)’
 void g (A&);
      ^

Reply via email to