Re: Inheritance based type conversion and private inheritance
Am 25.04.2011 14:55, schrieb Matthias Hofmann:
"Daniel Kr?gler"<daniel.kruegler@googlemail.com> schrieb im Newsbeitrag
news:ip0v9p$hi7$1@dont-email.me...
[..]
The compiler is defect and should not accept your very last
initialization.
That's good news because I was already beginning to doubt my sanity. I don't
know whether this is a known issue, maybe I should file a report with
Microsoft.
I recommend to do so, I verified that the same defect still exists in
VS2008. You may want to try VS2010 before reporting this.
I assume you are aware that above definition of a smart-pointer does not
ensure a single owner-ship responsibility and is therefore broken.
Yes, I only wrote the SmartPtr class to test inheritance based type
conversion, I am not actually using the class. That's why its also missing
operator->() and operator*(). This test was inspired by an article by Scott
Meyers, which uses a very similar implementation of a smart pointer class:
http://www.aristeia.com/Papers/C++ReportColumns/sep96.pdf
The article is only a draft, so it may not be entirely accurate, but what
confuses me is that he writes that in order for the member function template
that performs the type conversion to compile, it must be legal to convert
the pointer to the derived class to a pointer to a "public or protected"
base class. And as I did not understand how this is supposed to work with a
protected base class, I wrote my testing code and, much to my surprise,
found out that on my compiler, it even works with a private base class, but
now I know that this is a defect. Would it also be a defect if in my
example, Base was a protected base class of Derived, and the compiler would
not reject the code?
It would still be a defect, because the conversion happens not within
the derived class or a derived class thereof. According to 4.10
[conv.ptr] the pointer conversions requires that the base class is
accessible at that point, referring to Clause 11. According to 11.2
[class.access.base] protected base classes are only accessible in
friends or derived classes. More precisely, none of the criteria listed
in the bulleted list of paragraph 4 would apply here to make the base
class accessible. Due to some core issues, [class.access.base] p. 4 had
been reworded since C++ 2003, but this example was not affected by these
changes as far as I can see and should have been a defect in a
2003-conforming C++ compiler already.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]