c++ - 如何从按值返回的函数初始化 std::shared_ptr?

标签 c++ shared-ptr

我是这样做的:

class Something;
Something f();
...
std::shared_ptr<Something> ptr(new Something(f()));

但这感觉不对。此外,它需要复制构造函数。有没有更好的办法?

最佳答案

使用std::make_shared避免显式调用 new。同样,使用 std::make_unique

make_shared 可能更高效,因为它可以将智能指针和对象的计数器一起分配在一个 block 中。

不过,在构造对象之后但在安全地嵌入其智能指针之前,您至少还有一种方法可以让您的语句引发异常,否则它不会发挥作用。否则,上述异常会导致内存泄漏。

不良行为示例:

void f(std::shared_ptr<int> a, std::shared_ptr<int> b);

f(std::shared_ptr<int>(new int(0)), std::shared_ptr<int>(new int(4)));

并更正:

f(std::make_shared<int>(0), std::make_shared<int>(4));

现在,有人建议您不要按值返回 Something,而是作为动态分配的指针返回。对于您的用例,由于复制省略,只要 Something 是可复制的,实际上与可接受的编译器没有区别,也就是直接在 new< 分配的空间中构造返回值/make_shared/make_unique.
所以,只做您认为最好的事情。

标准明确允许复制省略。请注意复制构造函数无论如何都必须可以访问:

12.8. Copying and moving class objects §32

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.123 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
— in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object
— when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
— when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.

关于c++ - 如何从按值返回的函数初始化 std::shared_ptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475776/

相关文章:

c++ - DirectX 和 C++

c++ - 在前向声明的类层次结构的 shared_ptr 之间进行转换

c++ - 如何使用 STL 容器来保存基于模板的 shared_ptr?

c++ - 带有模板的 shared_ptr

c++ - 为什么重命名函数后出现链接器错误?

c++ - mfc c++ 中是否存在用于 URL 编码的 API?

c++ - 使用 make 将 .o 文件移动到单独的目录

C++ vector 引用参数

c++ - 如何在不使用 shared_ptr 的情况下不删除指针的值

c++ - 在 shared_ptr 过期后定位 weak_ptr