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

            Bug ID: 118393
           Summary: GCC does not allow calling of overloaded private type
                    conversion operator in default argument field of
                    friend function
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wangbopku15 at gmail dot com
  Target Milestone: ---

The following code does not compile in GCC:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class X {
  friend void friend_set (int);
  operator int() {return 0;};
};

void friend_set(int i=X()){}
void f()
{
  X obj;
  friend_set ();
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The compiler complains that the calling of 'X::operator int()' in the default
argument field is inaccessible:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>:9:23: error: 'X::operator int()' is private within this context
    9 | void friend_set(int i=X()){}
      |                       ^~~
<source>:3:3: note: declared private here
    3 |   operator int() {return 0;};
      |   ^~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

https://timsong-cpp.github.io/cppwp/n4868/class.access#general-8 says that 'The
names in a default argument ([dcl.fct.default]) are bound at the point of
declaration, and access is checked at that point rather than at any points of
use of the default argument.'. It seems this should not be rejected.

Also, note that GCC accepts the following case where the default argument field
tries to access a regular private member instead of an overloaded operator:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class X {
  friend void friend_set (int);
  int a;
};
//this is ok
void friend_set (int i=X().a) {}

void f()
{
  X obj;
  friend_set ();
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Clang, EDG, and ICC accept the code. Please see https://godbolt.org/z/nabdxG9nM
  • [Bug c++/118393] New: GCC does n... wangbopku15 at gmail dot com via Gcc-bugs

Reply via email to