【发布时间】:2019-05-24 10:13:21
【问题描述】:
“Downcasting” unique_ptr< Base > to unique_ptr< Derived > 为向下转换 unique_ptr 提供了一个优雅的解决方案。它在大部分时间都有效。但是当 Derived 包含 unique_ptr 时,就会出错:
template<typename Derived, typename Base, typename Del>
std::unique_ptr<Derived, Del>
static_unique_ptr_cast( std::unique_ptr<Base, Del>&& p )
{
auto d = static_cast<Derived *>(p.release());
return std::unique_ptr<Derived, Del>(d, std::move(p.get_deleter()));
}
struct Data
{
int data;
};
struct Base
{
};
struct Derived : public Base
{
Derived()
: data(std::make_unique<Data>())
std::unique_ptr<Data> data;
};
int main()
{
std::unique_ptr<Base> base = std::make_unique<Derived>();
auto data = static_unique_ptr_case<Derived>(std::move(base))->data; // compile error
return 0;
}
有没有更好的方法来解决这个问题?
开斋节:
修正错别字和
@Igor Tandetnik 给出解决方案
std::unique_ptr<Base> base = std::make_unique<Derived>();
//auto& data = static_unique_ptr_case<Derived>(std::move(base))->data; // compile error
auto derived = static_unique_ptr_case<Derived>(std::move(base));
auto& data = derived->data;
return 0;
【问题讨论】:
-
除了拼写错误(
Derived构造函数没有正确定义,缺少;等),问题是因为std::unique_ptr有一个明确的deleted 复制构造函数,并且有问题的错误是因为试图复制构造std::unique_ptr<Data>。在不知道为什么您希望复制构造std::unique_ptr工作的情况下 - 因为按照设计它不能 - 没有人能真正帮助你。 -
您的程序归结为
std::unique_ptr<Data> data; auto derived = data;失败与static_unique_ptr_cast无关。在修正了许多明显的错别字后,this code compiles -
@Igor Tandetnik 谢谢。我明白了。
标签: c++ c++11 smart-pointers unique-ptr