type_traits for checking derived type

Hi, some code: template <typename DerivedT> class A { BOOST_STATIC_ASSERT((boost::is_convertible<A<DerivedT>*, B*>::value)); [...] }; class B : public A<B> { }; I want to check if the template parameter really specifies a derived type. But the assert is always false. How can i solve this? bye

some code:
template <typename DerivedT> class A { BOOST_STATIC_ASSERT((boost::is_convertible<A<DerivedT>*, B*>::value)); [...] };
class B : public A<B> { };
I want to check if the template parameter really specifies a derived type. But the assert is always false. How can i solve this?
I think your code should be: template <typename DerivedT> class A { BOOST_STATIC_ASSERT((::boost::is_convertible<DerivedT*, A<DerivedT>*>::value)); // this should work as well: BOOST_STATIC_ASSERT((::boost::is_base_and_derived<A<DerivedT>, DerivedT>::value)); }; class B : public A<B> { }; However, the static asserts still always get triggered, and I'm not sure why: any language lawyers understand this? John.

"John Maddock" <john@johnmaddock.co.uk> writes:
some code:
template <typename DerivedT> class A { BOOST_STATIC_ASSERT((boost::is_convertible<A<DerivedT>*, B*>::value)); [...] };
class B : public A<B> { };
I want to check if the template parameter really specifies a derived type. But the assert is always false. How can i solve this?
I think your code should be:
template <typename DerivedT> class A { BOOST_STATIC_ASSERT((::boost::is_convertible<DerivedT*, A<DerivedT>*>::value)); // this should work as well: BOOST_STATIC_ASSERT((::boost::is_base_and_derived<A<DerivedT>, DerivedT>::value)); };
class B : public A<B> { };
However, the static asserts still always get triggered, and I'm not sure why: any language lawyers understand this?
Because B is an incomplete class in A's body, so the inheritance is invisible. It's complete inside of A's member functions; you could do the assert there... -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

John Maddock wrote:
some code:
template <typename DerivedT> class A { BOOST_STATIC_ASSERT((boost::is_convertible<A<DerivedT>*, B*>::value)); [...] };
class B : public A<B> { };
I want to check if the template parameter really specifies a derived type. But the assert is always false. How can i solve this?
I think your code should be:
template <typename DerivedT> class A { BOOST_STATIC_ASSERT((::boost::is_convertible<DerivedT*, A<DerivedT>*>::value)); // this should work as well: BOOST_STATIC_ASSERT((::boost::is_base_and_derived<A<DerivedT>, DerivedT>::value)); };
class B : public A<B> { };
However, the static asserts still always get triggered, and I'm not sure why: any language lawyers understand this?
At the instantiation point B and A<B> are still incomplete.

Peter Dimov wrote:
John Maddock wrote:
However, the static asserts still always get triggered, and I'm not sure why: any language lawyers understand this?
At the instantiation point B and A<B> are still incomplete.
Would it help to move the BOOST_STATIC_ASSERT to the dtor of A<B>? Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daniel Frey <daniel.frey@aixigo.de> writes:
Peter Dimov wrote:
John Maddock wrote:
However, the static asserts still always get triggered, and I'm not sure why: any language lawyers understand this? At the instantiation point B and A<B> are still incomplete.
Would it help to move the BOOST_STATIC_ASSERT to the dtor of A<B>?
Yes, if it meets the design goals fo the author. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (5)
-
D-Swat
-
Daniel Frey
-
David Abrahams
-
John Maddock
-
Peter Dimov