C++ 基于智能指针的CRTP习惯用法的编译问题
我试图为中给出的CRTP示例编译一个最小的工作示例,该示例基于智能指针 根据代码示例,我编写了两个文件,一个头文件和源文件 标题(C++ 基于智能指针的CRTP习惯用法的编译问题,c++,crtp,C++,Crtp,我试图为中给出的CRTP示例编译一个最小的工作示例,该示例基于智能指针 根据代码示例,我编写了两个文件,一个头文件和源文件 标题(crtp.h): 编译此代码失败,出现以下错误: In file included from example.cc:3: ./crtp.h:18:7: error: explicit specialization of non-template class 'CloneInherit' class CloneInherit<Derived, Base>:
crtp.h
):
编译此代码失败,出现以下错误:
In file included from example.cc:3:
./crtp.h:18:7: error: explicit specialization of non-template class 'CloneInherit'
class CloneInherit<Derived, Base>: public Base
^ ~~~~~~~~~~~~~~~
./crtp.h:29:16: error: no matching constructor for initialization of 'Concrete'
return new Derived(*this);
^ ~~~~~
./crtp.h:33:7: note: in instantiation of member function 'CloneInherit<Concrete, Cloneable>::clone_raw' requested here
class Concrete: public CloneInherit<Concrete, Cloneable>
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4411:26: note: in instantiation of member function 'std::__1::__shared_ptr_emplace<Concrete, std::__1::allocator<Concrete> >::__shared_ptr_emplace' requested
here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4775:29: note: in instantiation of function template specialization 'std::__1::shared_ptr<Concrete>::make_shared<>' requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
^
example.cc:7:42: note: in instantiation of function template specialization 'std::__1::make_shared<Concrete>' requested here
std::shared_ptr<Concrete> c = std::make_shared<Concrete>();
^
./crtp.h:33:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const CloneInherit<Concrete, Cloneable>' to 'const Concrete' for 1st argument
class Concrete: public CloneInherit<Concrete, Cloneable>
^
./crtp.h:33:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const CloneInherit<Concrete, Cloneable>' to 'Concrete' for 1st argument
class Concrete: public CloneInherit<Concrete, Cloneable>
^
./crtp.h:33:7: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
2 errors generated.
但我不确定这是否会得到与文章最初预期相同的结果。他的文章中确实有几个拼写错误: 固定版本:
#include <memory>
class cloneable
{
public:
virtual ~cloneable() {}
std::unique_ptr<cloneable> clone() const
{
return std::unique_ptr<cloneable>(this->clone_impl());
}
private:
virtual cloneable * clone_impl() const = 0;
};
template <typename Derived, typename Base>
class clone_inherit : public Base
{
public:
std::unique_ptr<Derived> clone() const
{
return std::unique_ptr<Derived>(static_cast<Derived*>(this->clone_impl()));
}
private:
clone_inherit* clone_impl() const override
{
return new Derived(*static_cast<const Derived*>(this));
}
};
class concrete : public clone_inherit<concrete, cloneable>
{
};
int main()
{
std::unique_ptr<concrete> c = std::make_unique<concrete>();
std::unique_ptr<concrete> cc = c->clone();
cloneable * p = c.get();
std::unique_ptr<cloneable> pp = p->clone();
}
#包括
类可克隆
{
公众:
虚拟~cloneable(){}
std::unique_ptr clone()常量
{
返回std::unique_ptr(this->clone_impl());
}
私人:
虚拟可克隆*clone_impl()常量=0;
};
模板
类clone\u inherit:public Base
{
公众:
std::unique_ptr clone()常量
{
返回std::unique_ptr(静态_cast(this->clone_impl());
}
私人:
clone\u inherit*clone\u impl()常量覆盖
{
返回新的派生(*static_cast(this));
}
};
类具体:公共克隆_继承
{
};
int main()
{
std::unique_ptr c=std::make_unique();
std::unique_ptr cc=c->clone();
可克隆*p=c.get();
std::unique_ptr pp=p->clone();
}
您确实应该删除
,因为您在这里不是专门化的,而是定义类。在clone\u inherit
的clone\u impl()
方法中强制转换为派生*
的原因是什么?此指针作为clone\u inherit*
返回到clone()
,但随后需要再次转换为派生的*
,这似乎是多余的。我缺少什么?我们确实想要一个签名返回类型派生*
,但是,派生类的定义还不是一个完整的类型,所以我们不能说派生类继承自基(对于协方差返回类型)clone\u inherit
继承Base
,因此它是有效的返回类型。然后,强制转换将修复键入:在该CRTP中,所有clone\u inherit
都是派生的基类。
In file included from example.cc:3:
./crtp.h:18:7: error: explicit specialization of non-template class 'CloneInherit'
class CloneInherit<Derived, Base>: public Base
^ ~~~~~~~~~~~~~~~
./crtp.h:29:16: error: no matching constructor for initialization of 'Concrete'
return new Derived(*this);
^ ~~~~~
./crtp.h:33:7: note: in instantiation of member function 'CloneInherit<Concrete, Cloneable>::clone_raw' requested here
class Concrete: public CloneInherit<Concrete, Cloneable>
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4411:26: note: in instantiation of member function 'std::__1::__shared_ptr_emplace<Concrete, std::__1::allocator<Concrete> >::__shared_ptr_emplace' requested
here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4775:29: note: in instantiation of function template specialization 'std::__1::shared_ptr<Concrete>::make_shared<>' requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
^
example.cc:7:42: note: in instantiation of function template specialization 'std::__1::make_shared<Concrete>' requested here
std::shared_ptr<Concrete> c = std::make_shared<Concrete>();
^
./crtp.h:33:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const CloneInherit<Concrete, Cloneable>' to 'const Concrete' for 1st argument
class Concrete: public CloneInherit<Concrete, Cloneable>
^
./crtp.h:33:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const CloneInherit<Concrete, Cloneable>' to 'Concrete' for 1st argument
class Concrete: public CloneInherit<Concrete, Cloneable>
^
./crtp.h:33:7: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
2 errors generated.
virtual CloneInherit* clone_raw() const override
{
return new CloneInherit(*this);
}
#include <memory>
class cloneable
{
public:
virtual ~cloneable() {}
std::unique_ptr<cloneable> clone() const
{
return std::unique_ptr<cloneable>(this->clone_impl());
}
private:
virtual cloneable * clone_impl() const = 0;
};
template <typename Derived, typename Base>
class clone_inherit : public Base
{
public:
std::unique_ptr<Derived> clone() const
{
return std::unique_ptr<Derived>(static_cast<Derived*>(this->clone_impl()));
}
private:
clone_inherit* clone_impl() const override
{
return new Derived(*static_cast<const Derived*>(this));
}
};
class concrete : public clone_inherit<concrete, cloneable>
{
};
int main()
{
std::unique_ptr<concrete> c = std::make_unique<concrete>();
std::unique_ptr<concrete> cc = c->clone();
cloneable * p = c.get();
std::unique_ptr<cloneable> pp = p->clone();
}