Re: Error when using Inheritance in templates.
On Aug 28, 1:11 am, Vinesh S <vinesh2...@gmail.com> wrote:
I wanted to use inheritance in the typename.
template <class T>
and in the main ...
Listener<BaseAdapter>* listenerPtr1 = new
where BaseAdapter -> ParentClass
ChildAdapter-> Child class derived from BaseAdapter.
You could write a generic constructor that either asserts failure or
uses SFINAE to disappear when inappropriate:
template < typename TT >
Listener(Listener<TT> const& other_type)
static_assert(is_convertible<TT,T>::value, "Only use with
I might have the argument order wrong with is_convertible. If you're
not using a C++0x compiler or are not ready to use that language, use
boost's type_traits library and BOOST_STATIC_ASSERT.
This one uses boosts enable_if.
template < typename TT>
Listener(Listener<TT> const& ot, typename
boost::enable_if<is_convertible<TT,T>>::type * = 0)
All of these constructs can be created yourself. The only one that is
mildly difficult is the is_convertible. You'll need the SFINAE
function overload trick if you have to do it yourself.
The key here is that the generic constructor will allow you to convert
from the unrelated type Listener<Derived> to Listener<Base>. The
safety measures allow you to do so secure that it'll only be used for
non-evil ends. They might not actually be necessary since attempting
to assign the ptr itself should blow up when it's an invalid
conversion. On the other hand, the static assert is slightly more
informative about what's going on