I am trying to write a template that will extract the base type of a boost::shared_ptr.
I wrote this template:
template<typename T>
struct ExtractBaseType;
template<typename T>
struct ExtractBaseType<boost::shared_ptr<T> >
{
typedef T type;
};
it works fine for a plain shared_ptr. This:
struct A
{
};
ExtractBaseType<boost::shared_ptr<A> >::type a_thing;
std::cout << typeid(a_thing).name() << std::endl;
prints “1A”.
However, this doesn’t compile:
struct B : boost::shared_ptr<A>
{
};
ExtractBaseType<B>::type b_thing;
The compiler complains that ExtractBaseType is undefined.
Why so? And how would this be done?
it doesn’t work because you are matching
shared_ptrnotB. you need to match derived ofshared_ptr.^ didn’t test, but the main idea is there
Good question. That said, inheriting from
shared_ptrseems ugly.