gpt4 book ai didi

c++ - 基于智能指针的 CRTP 习惯用法的编译问题

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:04 25 4
gpt4 key购买 nike

我正在尝试为 this blog post 中给出的 CRTP 示例编译一个最小的工作示例,它基于智能指针。

根据代码示例,我编写了两个文件,一个 header 和源代码。

header (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.

我可以通过多种方式修复这些错误。我可以删除 <Derived, Base>来自 CloneInherit 声明的特化使第一个错误消失并更改 clone_raw() 的定义CloneInherit的功能到

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

Demo

关于c++ - 基于智能指针的 CRTP 习惯用法的编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46915833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com