|
Boost : |
From: Jason Shirk (jasonsh_at_[hidden])
Date: 2002-01-11 16:18:44
I like this. A couple of things to consider:
* The standard does not require a vfptr in each instance of the class.
I'm not aware of any implementation that does not, but it is
theoretically possible to implement (maybe in interpreted environment?)
* is_polymorphic should return false for scalar types rather than fail
to compile.
Jason Shirk
VC++ Compiler Team
> -----Original Message-----
> From: John Maddock [mailto:John_Maddock_at_[hidden]]
> Sent: Friday, January 11, 2002 4:16 AM
> To: INTERNET:boost_at_[hidden]
> 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