Bug 29332 - default argument access checking is not done at the point where the default argument expression appears
Summary: default argument access checking is not done at the point where the default a...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: 4.8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2006-10-03 13:58 UTC by James Widman
Modified: 2021-08-02 05:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.1
Known to fail: 2.95, 3.2.3, 3.4.0, 4.0.0, 4.1.2, 4.2.0, 4.5.3, 4.6.4, 4.7.1
Last reconfirmed: 2006-10-03 17:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description James Widman 2006-10-03 13:58:57 UTC
For the following case: 

// line 1
class A {};
class B : private A { };

extern B b;

void f( A & = b); // ERROR

A & a = b; // ERROR

... g++ issues only one error:

t.cpp:9: error: 'A' is an inaccessible base of 'B'

... but it should also complain about the default arg.

Incidentally, this example was boiled down from code in a real project.
Comment 1 Andrew Pinski 2006-10-03 16:05:21 UTC
We just don't error until you use the function, in that the following errors:
// line 1
class A {};
class B : private A { };

extern B b;

int f( A & = b); // ERROR

A & a = b; // ERROR

int x = f(); // ERROR
Comment 2 James Widman 2006-10-03 16:38:20 UTC
Ok, here's something that's a little closer to the original example: 

class A; 
class B;

extern B b; 

struct C  { 
    static int f( A& = b); // ERROR
};

class A{};
class B : public A {};

int n = C::f();

Failing to report this error doesn't appear to lead to a "real" bug, but it's still ill-formed, so when you try to port your code to a compiler with the EDG front end, you'll get:

error: a reference of type "A &" (not const-qualified) cannot
          be initialized with a value of type "B"
      static int f( A& = b); // ERROR
                         ^
Comment 3 Andrew Pinski 2006-10-03 16:45:50 UTC
(In reply to comment #2)
> Ok, here's something that's a little closer to the original example: 
I get an error with that example:
t.cc:8: error: default argument for parameter of type ‘A&’ has type ‘B’

In fact the following is valid code which is why we cannot error until you actually call f:
// line 1
class A {};
class B : private A { friend void g(void);};

extern B b;

void f( A & = b); // ERROR

void g(void)
{
  f();
}
Comment 4 Andrew Pinski 2006-10-03 16:51:30 UTC
Reopening for a second.
Comment 5 Andrew Pinski 2006-10-03 16:58:22 UTC
Actually I take back comment #3.  This code is invalid as it violates 8.3.6/5:
"The names in the expressions are bound, and the semantics constraints are checked, at the point where the default argument expression appears.
Comment 6 Andrew Pinski 2006-10-03 17:00:00 UTC
Confirmed, not a regression.
Comment 7 Andrew Pinski 2021-08-02 05:00:21 UTC
Fixed in GCC 4.8.0 by r0-118020.

g++.dg/overload/defarg6.C is similar but not exactly the same.