Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-04-29 19:11:53


>From: "Philip Dunstan" <phil_at_[hidden]>

> Hi there,
>
> Should this work?

No. Template type deduction uses an object's static (declared) type, so in
this program, T=A, since the static type of pa is A*. That its dynamic type
is B* isn't taken into consideration, as template instantiation is done at
compile-time.

Regards,

Terje

> I've tried it using g++ 3.2 (redhat and cygwin) and borland c++ builder
> 5.6.4 and it fails on the assert for all of them.
>
> #include <boost/type_traits.hpp>
> #include <cassert>
>
> class A {};
>
> class B : public A {};
>
> template <typename T>
> bool isDerivedFromA(const T& t)
> {
> return boost::is_base_and_derived<A, T>::value;
> }
>
> int main()
> {
> B b;
> A* pa = &b;
> assert(isDerivedFromA(*pa));
> return 0;
> }


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