I am experimenting with Policies and CRTP and came across this issue.
So I am creating a SmartPtr
which along with all customisation points also have releasing policy (the rest I will skip for the sake of simplicity)
template<typename T>
class Releaser
{
int someDummyState;
public:
Releaser(int st):someDummyState(st) {}
void release()
{
cout << someDummyState << endl;
static_cast<T*>(this)->data = nullptr;
}
};
template<typename T, template<typename> typename Rel = Releaser>
class SmartPtr :
public Rel<SmartPtr<T,Rel>>
{
public:
SmartPtr(T* d,int val) :
Rel(val), // This line is error prone, Rel<SmartPtr>(val) solves this
data(d)
{}
T* operator*()
{
return data;
}
private:
friend class Releaser<SmartPtr>; // We need this as in the releaser we are accessing the private member
T* data;
};
int main()
{
SmartPtr<int> S(new int(3),8);
S.release();
}
So this gives me error C2512: 'Releaser<SmartPtr<int,Releaser>>': no appropriate default constructor available
But I don't see how I am invoking a default constructor for the Rel? I believe I am explicitly passing the val to the constructor.
This issue is resolved the I specify the template parameter for Rel (Rel<SmartPtr>(val))
.
Can someone please explain why I need to specify the base type template parameter and why it was trying to invoke the default constructor?