Detect generic base class
Hi list, how can I detect if a class inherits from a generic class? E.g., template <class Derived> class base {}; class final: public base<final> {}; I would like to specialise a type trait for all classes deriving from base<whatever>, possibly using enable_if and is_base_of: template <class T, class Enable = void> class trait: boost::false_type {}; // What syntax here? Something like this (which does not work): template <class T, class Derived> class trait<T,typename boost::enable_if<boost::is_base_of<base<Derived>,T> >::type>: boost::true_type {}; The best I've come up with has been putting a typedef for Derived in base, and then using SFINAE: template <class Derived> class base { public: typename Derived derived; }; template <class T> class trait<T,typename boost::enable_if<boost::is_base_of<base<typename T::derived>,T>
::type>: boost::true_type {};
But this is ugly and I'm sure there are much better ways of doing this :) Thanks in advance, Francesco
2010/4/29 Francesco Biscani <bluescarni@gmail.com>
Hi list,
how can I detect if a class inherits from a generic class? E.g.,
template <class Derived> class base {};
class final: public base<final> {};
is_instance_of from lambda library is what you want (see boost/lambda/detail/is_instance_of.hpp). It's not part of public interface though, so either copy it to your location or use on your own risk. Roman Perepelitsa.
Hi Roman, On Thu, Apr 29, 2010 at 9:23 AM, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
2010/4/29 Francesco Biscani <bluescarni@gmail.com>
Hi list,
how can I detect if a class inherits from a generic class? E.g.,
template <class Derived> class base {};
class final: public base<final> {};
is_instance_of from lambda library is what you want (see boost/lambda/detail/is_instance_of.hpp). It's not part of public interface though, so either copy it to your location or use on your own risk. Roman Perepelitsa.
thanks for the pointer, that's exactly it. However, I think I will end up deriving the base class from a non-templated tag class and use is_base_of<T,tag> instead - I do not think I'd be able to maintain my own version is_instance_of should it be changed/removed. Cheers, Francesco.
I did that a short time ago, by giving my generic base class a non-template base of its own. That's a good idea anyway, as you can hoist up anything that doesn't depend on the template type.
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Francesco Biscani Sent: Wednesday, April 28, 2010 9:31 PM To: boost-users@lists.boost.org Subject: [Boost-users] Detect generic base class
Hi list,
how can I detect if a class inherits from a generic class? E.g.,
template <class Derived> class base {};
TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
participants (3)
-
Francesco Biscani
-
John Dlugosz
-
Roman Perepelitsa