unique-ptr

Friend function is unable to construct a unique pointer of the class

梦想的初衷 提交于 2019-12-21 03:39:28
问题 I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to create a unique_pointer of my class using std::make_unique but it doesn't compile. My VC12 compiler complains c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1639): error C2248: 'Spam::Spam' : cannot access private member declared in class 'Spam' The relevant code which fails during compilation is as

unique_ptr autocomplete in eclipse

六眼飞鱼酱① 提交于 2019-12-21 02:36:07
问题 I am playing with unique_ptr. In my last post people helped me compiling a program that used this pointer by specifying the -std=c++0x during compilation. Now i was wondering if there is any way to instruct eclipse to consider c++11 while auto-completing? unique_ptr is not coming in the list of std:: namespace, nor I can find the methods (reset, move...) associated with a unique_ptr. Thank you vahid 回答1: The "memory" header (probably found at /usr/include/c++/4.9/memory) only includes "unique

About unique_ptr performances

夙愿已清 提交于 2019-12-20 03:17:05
问题 I often read that unique_ptr would be preferred in most situations over shared_ptr because unique_ptr is non-copyable and has move semantics; shared_ptr would add an overhead due to copy and ref-counting; But when I test unique_ptr in some situations, it appears it's noticably slower (in access) than its counterparts For example, under gcc 4.5 : edit : the print method doesn't print anything actually #include <iostream> #include <string> #include <memory> #include <chrono> #include <vector>

Why does the insertion of user defined destructor require an user defined copy constructor

偶尔善良 提交于 2019-12-20 02:44:15
问题 The following code compiles: #include <vector> #include <iostream> #include <memory> using namespace std; class container { public: container(){} ~container(){} }; class Ship { public: Ship(){} //Ship(const Ship & other){cout<<"COPY"<<endl;} //~Ship(){} std::unique_ptr<container> up; }; Ship buildShip() { Ship tmp; return tmp; } int main(int argc, char *argv[]) { return 0; } But if we include the user defined destructor ~Ship(){} , the code will only compile if we also include the user

“No-throw dereferencing” of std::unique_ptr

守給你的承諾、 提交于 2019-12-19 17:41:44
问题 I write code in C++ which uses a std::unique_ptr u to handle a std::string resource, and I want to dereference u so that I can pass the std::string to a call of the std::string copy constructor: std::string* copy = new std::string( /*dereference u here*/ ); I know that new or the std::string copy constructor could throw, but this is not my point here. I was just wondering whether dereferencing u could already throw an exception. I find it strange that operator* is not marked noexcept while

“No-throw dereferencing” of std::unique_ptr

喜你入骨 提交于 2019-12-19 17:41:44
问题 I write code in C++ which uses a std::unique_ptr u to handle a std::string resource, and I want to dereference u so that I can pass the std::string to a call of the std::string copy constructor: std::string* copy = new std::string( /*dereference u here*/ ); I know that new or the std::string copy constructor could throw, but this is not my point here. I was just wondering whether dereferencing u could already throw an exception. I find it strange that operator* is not marked noexcept while

Unique pointer in-class initialization

浪尽此生 提交于 2019-12-19 08:11:21
问题 Suppose I have a unique_ptr member object that I want to initialize in-class, see the code below. Why do I have to use uniform initialization (curly braces)? The second declaration spits an error, something like so.cpp:10:31: error: expected parameter declarator std::unique_ptr<Foo> upf2(new Foo); ^ so.cpp:10:31: error: expected ')' so.cpp:10:30: note: to match this '(' std::unique_ptr<Foo> upf2(new Foo); ^ 2 errors generated. And I don't think is a most vexing parse issue, at least I don't

Add implicit conversion from unique_ptr<T> to T*

≯℡__Kan透↙ 提交于 2019-12-19 06:19:16
问题 General Question: Without going into whether or not it's a good idea, how can I add an implicit conversion operator to a class that has already been defined? For example, let's say that I want unique_ptr<T> to implicitly convert to T* , but I can't just add a member conversion operator because I can't change the definition of the unique_ptr class. Options: Is there some c++ voodoo that I can use to make this happen without creating a member function? Answer-So-Far: NO. There is no way to add

unique_ptr<int[]> or vector<int>?

女生的网名这么多〃 提交于 2019-12-18 15:17:54
问题 If you don't need dynamic growth and don't know the size of the buffer at compile time, when should unique_ptr<int[]> be used instead of vector<int> if at all? Is there a significant performance loss in using vector instead of unique_ptr ? 回答1: There is no performance loss in using std::vector vs. std::unique_ptr<int[]> . The alternatives are not exactly equivalent though, since the vector could be grown and the pointer cannot (this can be and advantage or a disadvantage, did the vector grow

Difference between boost::scoped_ptr<T> and std::unique_ptr<T>

心不动则不痛 提交于 2019-12-18 10:46:46
问题 Is the sole difference between boost::scoped_ptr<T> and std::unique_ptr<T> the fact that std::unique_ptr<T> has move semantics whereas boost::scoped_ptr<T> is just a get/reset smart pointer? 回答1: No, but that is the most important difference. The other major difference is that unique_ptr can have a destructor object with it, similarly to how shared_ptr can. Unlike shared_ptr , the destructor type is part of the unique_ptr 's type (the way allocators are part of STL container types). 回答2: