2017-10-24 10 views
1

스마트 포인터를 기반으로하는 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); 
} 

CloneInheritclone_raw() 함수의 정의를 변경하기 위해 CloneInherit에 대한 선언에서 <Derived, Base> 전문화를 삭제할 수 있지만, 나는 이것이 무엇인지와 같은 결과를 얻을 수 있는지 확실하지 않습니다 게시물 원래 의도.

고정 버전 :

+1

당신은 참으로 제거해야합니다'<파생, 자료> '당신이 여기에 전문으로하지 않는 한, 그러나 클래스를 정의하십시오. – Jarod42

답변

0

실제로 자신의 게시물에 몇 가지 오타가 있습니다

#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(); 
} 

Demo

+0

'clone_inherit'의'clone_impl()'메쏘드에서'파생 된'에 캐스트하는 이유는 무엇입니까? 이 포인터는'clone()'에'clone_inherit *'로 반환되었지만'Derived * '에 다시 캐스팅 될 필요가있다. 내가 뭘 놓치고 있니? –

+0

우리는 실제로 서명 반환 유형'Derived * '을 원하지만 Derived 클래스의 정의는 아직 완전한 유형이 아니므로 Derived가 Base에서 상속한다고 (공분산 반환 유형의 경우) 말할 수는 없습니다. 'clone_inherit'는'Base'를 상속 받았기 때문에 유효한 반환 타입입니다. 그런 다음 CRTP에서 모든 'clone_inherit'은 'Derived'의 기본 클래스입니다. – Jarod42