首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::make_unique和unique_ptr

std::make_unique和unique_ptr
EN

Stack Overflow用户
提问于 2021-08-27 08:37:47
回答 2查看 220关注 0票数 1

如果我没看错的话,cppreference说以下两个构造应该等同于C++14和更高版本中的非数组类型:

代码语言:javascript
复制
make_unique<T>(T());
unique_ptr<T>(new T());

很明显,我没有理解一个微妙的地方。下面是一个例子(包含头文件和脚手架的完整文件是here。在本例中,我希望cmp3cmp4具有相同的效果,但cmp4无法编译。

代码语言:javascript
复制
template <typename T>
class DbCell {
   public:
    DbCell() {}
    DbCell(const T& v) : value_(v.value_) {}
    DbCell(T&& v) : value_(move(v.value_)) {}
    ~DbCell() {}
   private:
    T value_;
};

struct ChapterStats {
    ChapterStats() {}
};
class ChapterMap {
   public:
    ChapterMap() {}
    ChapterMap(const ChapterMap&) = delete;
    ChapterMap(ChapterMap&& cm) : the_map_(move(cm.the_map_)) {}

   private:
    map<string, unique_ptr<ChapterStats>> the_map_;
};
void foo() {
    DbCell<ChapterMap> cm;

    unique_ptr<DbCell<int>> cmp1 =
        make_unique<DbCell<int>>(DbCell<int>());

    unique_ptr<ChapterMap> cmp2 = make_unique<ChapterMap>(ChapterMap());

    unique_ptr<DbCell<ChapterMap>> cmp3 =
        unique_ptr<DbCell<ChapterMap>>(new DbCell<ChapterMap>());

    // This next fails, even though I think it should be equivalent to cmp3.
    cout << "> cmp4:" << endl;
    unique_ptr<DbCell<ChapterMap>> cmp4 =
        make_unique<DbCell<ChapterMap>>(new DbCell<ChapterMap>());
}

如果没有cmp4,输出是这样的(添加适当的打印语句):

代码语言:javascript
复制
Constructed ChapterMap.
Constructed DbCell.
> cmp1:
Constructed DbCell.
> cmp2:
Constructed ChapterMap.
Moved ChapterMap.
> cmp3:
Constructed ChapterMap.
Constructed DbCell.

对于cmp4,编译器错误是这样的:

代码语言:javascript
复制
In file included from rgr.cc:6:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/memory:81:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/unique_ptr.h:765:34: error: no matching constructor for initialization of 'DbCell<ChapterMap>'
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                                 ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
rgr.cc:61:9: note: in instantiation of function template specialization 'std::make_unique<DbCell<ChapterMap>, DbCell<ChapterMap> *>' requested here
        make_unique<DbCell<ChapterMap>>(new DbCell<ChapterMap>());
        ^
rgr.cc:18:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'DbCell<ChapterMap> *' to 'const DbCell<ChapterMap>' for 1st argument; dereference the argument with *
class DbCell {
      ^
rgr.cc:21:5: note: candidate constructor not viable: no known conversion from 'DbCell<ChapterMap> *' to 'const ChapterMap' for 1st argument
    DbCell(const T& v) : value_(v.value_) {
    ^
rgr.cc:24:5: note: candidate constructor not viable: no known conversion from 'DbCell<ChapterMap> *' to 'ChapterMap' for 1st argument
    DbCell(T&& v) : value_(move(v.value_)) { cout << "Moved DbCell." << endl; }
    ^
rgr.cc:20:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    DbCell() { cout << "Constructed DbCell." << endl; }
    ^
1 error generated.

令我困惑的一件事是编译器抱怨隐式删除的DbCell<ChapterMap>复制构造函数,但我已经定义了一个(我不需要它,只是为了示例)。

EN

回答 2

Stack Overflow用户

发布于 2021-08-27 08:42:05

这两个并不完全相同:

代码语言:javascript
复制
   make_unique<T>(T());
   unique_ptr<T>(new T());

这两个是相同的:

代码语言:javascript
复制
   make_unique<T>();
   unique_ptr<T>(new T());

在第一个示例中,您有一个额外的移动/复制ctor,它涉及到make_unique<T>的调用。

票数 9
EN

Stack Overflow用户

发布于 2021-08-27 09:03:01

对于cmp4,您应该使用

代码语言:javascript
复制
unique_ptr<DbCell<ChapterMap>> cmp4 =
        make_unique<DbCell<ChapterMap>>(/*empty*/);

std::make_unique调用其类型的构造函数,在本例中,该构造函数将是一个接受指向DbCell的指针的构造函数,而您没有该构造函数。编译器错误消息突出显示了这一点:

代码语言:javascript
复制
rgr.cc:18:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'DbCell<ChapterMap> *' to 'const DbCell<ChapterMap>' for 1st argument; dereference the argument with *
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68950432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档