This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Protected constructor access rules changed?
>>>>> "Rick" == Rick Campbell <campbell@cyberpass.net> writes:
Rick> Date: Wed, 4 Feb 1998 08:57:52 GMT From: Mark Mitchell
Rick> <mmitchell@usa.net>
Rick> Constructors may not be called by name, except in the
Rick> initializers portion of a derived class constructor. I
Rick> *think* this has been true since the ARM, but I could be
Rick> wrong. So, I don't believe there is a bug here.
Rick> Hmmm. That may well be, but in a bit over 12 years of C++
Rick> hacking, I've never heard anyone suggest that before. While
Rick> I'm not as familiar with the current spec, I've been back
Rick> and forth through the ARM enough times that I'm pretty
Rick> confident that it does not disallow calling constructors by
Rick> name.
I apologize. I missed the point of your example, due to an overly
cursory reading. However, I still believe that egcs is correct, and
here is why:
[class.protected]
When a friend or a member function of a derived class references a
protected nonstatic member of a base class, an access check applies in
addition to those described earlier in this clause. Except when
forming a pointer to a member, the access must be through a pointer
to, reference to, or object of the derived class itself (or any object
derived from that class.)...
Example:
class B { protected int i; };
class D1 : public B { };
class D2 : public B { void mem(B*, D1*); };
void D2::mem(B* pb, D1* p1)
{
pb->i = 1; // ill-formed
p1->i = 2; // ill-formed
this->i = 3; // ok
}
I aplogoize for wasting your time with my earlier answer.
Rick> In fact paragraph 2 of 12.1 appears to explicitly allow such
Rick> a ``call'' -- technically considered a type conversion --
Rick> and paragraph 13A shows an example of making such a call:
Rick> 13A functional notation type conversion
Rick> (_expr.type.conv_) can be used to create new objects of its
Rick> type. [Note: The syntax looks like an explicit call of the
Rick> constructor. ] [Example: complex zz = complex(1,2.3);
Rick> cprint( complex(7.8,1.2) ); --end example] An object created
Rick> in this way is unnamed. [Note: _class.temporary_ describes
Rick> the lifetime of temporary objects. ] [Note: explicit
Rick> constructor calls do not yield lvalues, see _basic.lval_. ]
Rick> At the point, I feel much more confident in saying that
Rick> egcs's failure to compile the attached code is a true bug.
Rick> The is purely an access issue.
Rick> In any case, if what you say is true, that egcs has a
Rick> different bug in that it permits calling most constructors
Rick> by name. For example, in the code that I sent out
Rick> previously, if you move the first `protected' keyword from
Rick> just before to just after the the void* constructor, it
Rick> compiles without incident.
Rick> Rick
--
Mark Mitchell mmitchell@usa.net
Stanford University http://www.stanford.edu