Re: Sanity check: public/private
* James Kanze:
Alf P. Steinbach wrote:
* Carlos Moreno:
I'm not saying that #defining private as public is the way
to run test protocols; I simply think that it is an
interesting alternative, that can be quite useful in certain
situations -- I wanted to know if I should expect the trick
to work in the general case.
From a practical point of view I think so.
Provided he's not using private inheritance. (Even then, I'd be
surprised if he had a problem unless private inheritance was
used in the exception hierarchy. And only in exceptional cases
then.)
Truth be told I didn't think of that case, thanks! :-) It's subtle.
Illustration code, checking for conversion to inaccessible base at run-time:
#include <iostream>
#include <ostream>
#if 0
# define private public
#endif
void out( char const s[] ) { std::cout << s << std::endl; }
class IsSerializable {};
class Base {};
class Derived: public Base, private IsSerializable {};
template< class A, class B >
class IsConvertibleFromTo
{
static char (&convert( ... ))[1];
static char (&convert( B const* ))[2];
static A const* a();
public:
// Fails to compile if B is an inaccessible base class of A.
enum{ yes = (sizeof( convert(a()) ) == 2) };
};
template< class A, class B >
bool isConvertibleFromTo()
{
try { throw static_cast<A const*>( 0 ); }
catch( B const* ) { return true; }
catch( ... ) { return false; }
}
int main()
{
out( IsConvertibleFromTo<Derived, Base>::yes? "B" : "No B" );
out( isConvertibleFromTo<Derived, IsSerializable>()? "S" : "No
S" );
}
Change the "#if 0" to "#if 1" and the output of the program is changed.
Now the question for us language definition nit-pickers (I currently
doubt that my question has practical value),
* How can we can we check for conversion to an inaccessible base class
at compile time rather than run time?
Additional question: can this have practical value?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]