Reputation: 187
Consider the following pointers class templates :
// PointerTemplates.hpp
template <class T, class X = ClassX<T>, class Y = ClassY<T> >
class Smart_Ptr_A
{
// [...]
};
template <class T>
class Smart_Ptr_B
{
// [...]
};
template <class T, class Count = TSCount>
class Smart_Ptr_C
{
// [...]
};
I want to create other smart pointers that correspond to those pointers but where the member pointer isn't the object inside "<>", but act as it was. Something like:
template<class PointedClass>
class Smart_Ptr_A_Modified
{
public:
// [...]
PointedClass& operator*() const
{ return m_pOtherClass->getPointedClass(); }
// OtherClass contains a PointedClass.
Smart_Ptr_A<OtherClass> m_pOtherClass;
};
Almost all methods and operator of the smart pointers are the same, so I want to use a template to implements the things they have in common and specialize them.
How could I do that ?
For now, I have something like the following:
// Common implementation
template< template<class> class PtrType, class PointedClass>
class CommonModifiedPtr
{
// implementation of things in common
// [...]
private:
PtrType<OtherClass> m_pOtherClass;
}
// Specialization
template<class PointedClass>
class Smart_Ptr_A_Modified
: CommonModifiedPtr<Smart_Ptr_A<OtherClass>, PointedClass>
{
public:
PointedClass* releaseControl()
{ return m_pOtherClass->releaseControl(); }
};
template<class PointedClass>
class Smart_Ptr_B_Modified
: CommonModifiedPtr<Smart_Ptr_B<OtherClass>, PointedClass>
{};
template<class PointedClass>
class Smart_Ptr_C_Modified
: CommonModifiedPtr<Smart_Ptr_C<OtherClass>, PointedClass>
{};
But I get the following error for each of the specialization:
error C3200: 'Smart_Ptr_A_Modified' : invalid template argument for template parameter 'PtrType', expected a class template
Additional information : I use MSVC++ 10.0 with C++98 and I only have little knowledge of templates.
Upvotes: 0
Views: 68
Reputation: 990
Inheritance is indeed the correct approach. In the following code:
template<class PointedClass>
class Smart_Ptr_X_Modified
: CommonModifiedPtr<Smart_Ptr_X<OtherClass>, PointedClass>
{ // ...
Should be replaced with:
template<class PointedClass>
class Smart_Ptr_X_Modified
: CommonModifiedPtr<Smart_Ptr_X, PointedClass>
{ // ...
With 'X' replaced by A,B,C respectively.
This is because Smart_Ptr_X<OtherClass>
is a specific class and not a class template.
Edit: The above might not be exactly what you want. This depends on what OtherClass
is and how you want it to fit into your code.
For a better answer, see this: https://stackoverflow.com/help/minimal-reproducible-example
Second edit: After the replacement above, the problem you encounter derives from the optional template arguments - The Smart_Ptr_X
classes expect different template parameters (never mind that some of them have default values), and handling them uniformly will be problematic.
One ugly solution I can suggest, is that for purposes of CommonModifiedPtr
you do not use Smart_Ptr_X
, and instead use something like:
template <class T>
class Simple_Smart_Ptr_X : Smart_Ptr_X<T> {};
Upvotes: 2