Can I have a template constructor inside a template class?
Hi,
This does not compile but it should since other is an instance of a class template which inherits from CheckingPolicy<T2>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
template
<
typename T,
template<typename> class CheckingPolicy
>
struct SmartPtr : public CheckingPolicy<T>
{
SmartPtr(T* p = nullptr) : pointee{ p } { }
template<typename T2, template<typename> class CP2>
SmartPtr(const SmartPtr<T2, CP2>& other)
: CheckingPolicy<T2>( other)
{
auto pCPT2 = static_cast<CheckingPolicy<T2>*>(&other);
}
template<typename T2, template<typename> class CP2>
friend struct SmartPtr;
};
|
The rest of the code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
/// ChekingPolicies
/// </summary>
/// <typeparam name="T"></typeparam>
template<typename T>
struct NoChecking
{
NoChecking() = default;
template<typename T2>
NoChecking(const NoChecking<T2>& other)
{}
static void Check(T*) {}
};
template<typename T>
struct EnforceNotNull
{
EnforceNotNull() = default;
template<typename T2>
EnforceNotNull(const EnforceNotNull<T2>& other)
{}
static void Check(T* p)
{
if (!p)
throw std::exception{ "Null Pointer Exception" };
}
};
struct Sample
{};
void usePolicy()
{
SmartPtr<Sample, EnforceNotNull> spSample(new Sample);
// copy constructor:
SmartPtr<Sample, NoChecking> spSample2(spSample);
}
|
The error is:
'static_cast': cannot convert from 'const SmartPtr<Sample,EnforceNotNull> *' to 'NoChecking<T> *'
|
What is going on?
Regards,
Juan
One is a pointer to const and the other is a pointer to non-const.
Topic archived. No new replies allowed.