Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-11 09:01:32


of COURSE!!

never mind that there's no reason this should work unless you know something
about the implementation; it's gonna work ;-)

The best part is that this won't even require compiler support on any
implementation we know of. It's in that convenient class of "non-portable"
extensions that any library implementor can easily provide.

-Dave

----- Original Message -----
From: "John Maddock" <John_Maddock_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, January 11, 2002 7:15 AM
Subject: [boost] type_traits proposal

>
> >One thing I want which I don't think is in the proposal is the ability to
> determine at compile-time whether or not a type is polymorphic.
>
> I am building a runtime representation of the inheritance hierarchy for
> type
> conversion in Boost.Python. For polymorphic types, I need to generate
> downcasts. As it stands, the interface is clumsy because the user has to
> tell the library whether a given base class is polymorphic.
>
> If a type X is not polymorphic, it is illegal to even write
>
> dynamic_cast<Y*>(&a)
>
> where typeid(a)==typeid(X), unless Y is a base class of X.
>
> Since it seems highly unlikely that we'll get the compile-time "tell me
> whether this expression would be an error" operator, we ought to be
> aggressive about pursuing things that help us avoid making errors in the
> first place ;-)
> <
>
> Funny you should mention that, I came up with a reasonably protable
> implementation for that the other day - I just haven't had a chance to try
> it out. Heres my rather quickly knocked up code:
>
> #include <boost/type_traits.hpp>
> #include <boost/type_traits/type_traits_test.hpp>
>
> template <class T>
> struct is_polymorphic
> {
> struct d1 : public T
> {
> d1();
> char padding[256];
> };
> struct d2 : public T
> {
> d2();
> virtual ~d2();
> virtual void foo();
> char padding[256];
> };
> BOOST_STATIC_CONSTANT(bool, value = (sizeof(d2) == sizeof(d1)));
> };
>
> int main(int argc, char* argv[])
> {
> value_test(false, is_polymorphic<Base>::value);
> value_test(false, is_polymorphic<Derived>::value);
> value_test(false, is_polymorphic<NonDerived>::value);
> value_test(true, is_polymorphic<VB>::value);
> value_test(true, is_polymorphic<VD>::value);
> return 0;
> }
> unsigned int expected_failures = 0;
>
> Works for Borland/gcc/VC6 and VC7.
>
> - John Maddock
> http://ourworld.compuserve.com/homepages/john_maddock/
>
> Info: http://www.boost.org Send unsubscribe requests to:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk