스마트 포인터를 기반으로하는 this blog post에 제공된 CRTP 예제에 대해 최소한의 작업 예제를 컴파일하려고합니다.스마트 포인터 기반 CRTP 숙어 컴파일 문제
코드 예를 기반으로 헤더와 소스라는 두 개의 파일을 작성했습니다.
헤더 (crtp.h
) :
#include <memory>
class Cloneable
{
public:
virtual ~Cloneable() {}
std::shared_ptr<Cloneable> clone() const
{
return std::shared_ptr<Cloneable>(this->clone_raw());
}
private:
virtual Cloneable* clone_raw() const = 0;
};
template <typename Derived, typename Base>
class CloneInherit<Derived, Base>: public Base
{
public:
std::shared_ptr<Derived> clone() const
{
return std::shared_ptr<Derived>(static_cast<Derived*>(this->clone_raw()));
}
private:
virtual CloneInherit* clone_raw() const override
{
return new Derived(*this);
}
};
class Concrete: public CloneInherit<Concrete, Cloneable> {};
소스 (example.cc
) :이 코드의
#include <memory>
#include "crtp.h"
int main()
{
std::shared_ptr<Concrete> c = std::make_shared<Concrete>();
std::shared_ptr<Concrete> cc = c->clone();
Cloneable* p = c.get();
std::shared_ptr<Cloneable> pp = p->clone();
return 0;
}
컴파일 다음과 같은 오류와 함께 실패
: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);
}
에 CloneInherit
의 clone_raw()
함수의 정의를 변경하기 위해 CloneInherit
에 대한 선언에서 <Derived, Base>
전문화를 삭제할 수 있지만, 나는 이것이 무엇인지와 같은 결과를 얻을 수 있는지 확실하지 않습니다 게시물 원래 의도.
고정 버전 :
당신은 참으로 제거해야합니다'<파생, 자료> '당신이 여기에 전문으로하지 않는 한, 그러나 클래스를 정의하십시오. – Jarod42