我是新手在C++ 11中移动语义,我不太清楚如何处理unique_ptr
构造函数或函数中的参数.考虑这个引用自身的类:
#include <memory>
class Base
{
public:
typedef unique_ptr<Base> UPtr;
Base(){}
Base(Base::UPtr n):next(std::move(n)){}
virtual ~Base(){}
void setNext(Base::UPtr n)
{
next = std::move(n);
}
protected :
Base::UPtr next;
};
Run Code Online (Sandbox Code Playgroud)
这是我应该如何编写unique_ptr
参数的函数?
我需要std::move
在调用代码中使用吗?
Base::UPtr b1;
Base::UPtr b2(new Base());
b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?
Run Code Online (Sandbox Code Playgroud) unique_ptr<T>
不允许复制构造,而是支持移动语义.然而,我可以unique_ptr<T>
从函数返回一个并将返回的值赋给变量.
#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int> foo()
{
unique_ptr<int> p( new int(10) );
return p; // 1
//return move( p ); // 2
}
int main()
{
unique_ptr<int> p = foo();
cout << *p << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码按预期编译和工作.那么该行如何1
不调用复制构造函数并导致编译器错误呢?如果我必须使用line 2
而不是它有意义(使用line 2
也可以,但我们不需要这样做).
我知道C++ 0x允许此异常,unique_ptr
因为返回值是一个临时对象,一旦函数退出就会被销毁,从而保证返回指针的唯一性.我很好奇这是如何实现的,它是在编译器中特殊的,还是在语言规范中有一些其他条款可以利用?
我在头文件中有一些代码如下:
#include <memory>
class Thing;
class MyClass
{
std::unique_ptr< Thing > my_thing;
};
Run Code Online (Sandbox Code Playgroud)
如果我有一个CPP这个头不包含的Thing
类型定义,那么这并不在VS2010 SP1的编译:
1> C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\VC\include\memory(2067):错误C2027:使用未定义类型'Thing'
替换std::unique_ptr
为std::shared_ptr
和编译.
所以,我猜这是当前VS2010 std::unique_ptr
的实现,需要完整的定义,而且完全依赖于实现.
或者是吗?它的标准要求中是否有某些东西使得std::unique_ptr
实施只能使用前向声明?感觉很奇怪,因为它应该只有一个指针Thing
,不应该吗?
有人可以解释shared_ptr和unique_ptr之间的差异吗?
为什么std::make_unique
标准C++ 11库中没有函数模板?我发现
std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3));
Run Code Online (Sandbox Code Playgroud)
有点冗长.以下不是更好吗?
auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
这隐藏得new
很好,只提到一次类型.
无论如何,这是我尝试实现make_unique
:
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
我花了很std::forward
长时间来编译这些东西,但我不确定它是否正确.是吗?究竟是什么std::forward<Args>(args)...
意思?编译器对此做了什么?
这个程序有什么问题?
#include <memory>
#include <vector>
int main()
{
std::vector<std::unique_ptr<int>> vec;
int x(1);
std::unique_ptr<int> ptr2x(&x);
vec.push_back(ptr2x); //This tiny command has a vicious error.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/memory:64,
from main.cpp:6:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::unique_ptr<int>, _Tp* = std::unique_ptr<int>*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:745:6: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const …
Run Code Online (Sandbox Code Playgroud) 随着新标准的出现(以及某些编译器中已有的部件),新类型std::unique_ptr
应该是替代品std::auto_ptr
.
它们的用法是否完全重叠(因此我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做))或者我应该注意一些在阅读文档时不明显的差异?
此外,如果它是一个直接替代品(为什么给它一个新的名称),而不仅仅是改善std::auto_ptr
.
我正在使用pimpl-idiom std::unique_ptr
:
class window {
window(const rectangle& rect);
private:
class window_impl; // defined elsewhere
std::unique_ptr<window_impl> impl_; // won't compile
};
Run Code Online (Sandbox Code Playgroud)
但是,我在第304行的第304行收到有关使用不完整类型的编译错误<memory>
:
'
sizeof
'到不完整类型'uixx::window::window_impl
的应用无效' '
据我所知,std::unique_ptr
应该可以使用不完整的类型.这是libc ++中的错误还是我在这里做错了什么?
我无法理解智能指针在C++ 11中作为类成员的用法.我已经阅读了很多关于智能指针的内容,我想我确实理解了如何unique_ptr
和shared_ptr
/ weak_ptr
一般的工作.我不明白的是真正的用法.似乎每个人都建议使用unique_ptr
几乎所有的时间.但是,我将如何实现这样的事情:
class Device {
};
class Settings {
Device *device;
public:
Settings(Device *device) {
this->device = device;
}
Device *getDevice() {
return device;
}
};
int main() {
Device *device = new Device();
Settings settings(device);
// ...
Device *myDevice = settings.getDevice();
// do something with myDevice...
}
Run Code Online (Sandbox Code Playgroud)
假设我想用智能指针替换指针.A unique_ptr
不会起作用getDevice()
,对吧?那是我使用的时间shared_ptr
和weak_ptr
?没办法用unique_ptr
?对我来说似乎对大多数情况shared_ptr
更有意义,除非我在一个非常小的范围内使用指针?
class Device {
};
class Settings {
std::shared_ptr<Device> device;
public:
Settings(std::shared_ptr<Device> …
Run Code Online (Sandbox Code Playgroud) 我有一个带有unique_ptr成员的类.
class Foo {
private:
std::unique_ptr<Bar> bar;
...
};
Run Code Online (Sandbox Code Playgroud)
Bar是第三方类,具有create()函数和destroy()函数.
如果我想std::unique_ptr
在独立功能中使用它,我可以这样做:
void foo() {
std::unique_ptr<Bar, void(*)(Bar*)> bar(create(), [](Bar* b){ destroy(b); });
...
}
Run Code Online (Sandbox Code Playgroud)
std::unique_ptr
作为班级成员,有没有办法做到这一点?
c++ ×10
unique-ptr ×10
c++11 ×9
shared-ptr ×2
stl ×2
arguments ×1
auto-ptr ×1
libc++ ×1
pointers ×1