|
Boost Users : |
From: Ian McCulloch (ianmcc_at_[hidden])
Date: 2006-10-06 12:55:56
Gennadiy Rozental wrote:
>
> "Stefan Schild" <schilds_at_[hidden]> wrote in message
> news:eg3r7s$bcb$1_at_sea.gmane.org...
>> Hi there
>>
>> Having a templatized base class B
>>
>> template<class T>
>> class B .....
>>
>> is it possible to enforce that a class D can derive from B IF AND ONLY
>> IF the curiously recurring template pattern is applied, that is
>>
>> class D1 : public B<D1> ... // ok, compile
>> class D2 : public B<X> ... // not ok, do not compile
>>
>> ???
>>
>> Thx
>> Stefan
>
> Did you try:
>
> template<typename T>
> class B {
> BOOST_STATIC_ASSERT( boost::is_base_and_derived<B<T>, T>::value );
> ....
> };
This doesn't quite work? At this point, T is incomplete. But, it ought to
be possible to put this check inside some member function of B; probably
the one that casts *this to T& would be the ideal place. But then it
should be pretty much unnecessary as the static_cast will fail if the
conversion is not possible. Stefan, can you give more details, i.e. a
*real* example where you want it to fail to compile, but it does?
Cheers,
Ian McCulloch
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net