unique-ptr

How can I have a pair with reference inside vector?

回眸只為那壹抹淺笑 提交于 2019-12-18 09:37:35
问题 I really need to make std::vector of std::pair with reference ( & ) inside, but it breaks inside the function when I try to push_back reference value. After debugging I discovered, that the address of reference is different from the address inside unique_ptr (but the value is the same). When I don't use (here the foo()) any function that insert into vector it's the value it refers to is correct, but the addresses still don't match. #include <iostream> #include <memory> #include <iterator>

Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr

独自空忆成欢 提交于 2019-12-18 06:57:48
问题 I have read through many questions on SO on custom deleter for shared_ptr and unique_ptr , and the difference between the two. But, I still haven't found any clear answer to this question: How can one best go about creating a type that acts as a shared_ptr with a custom deleter, similar to how unique_ptr has the deleter as part of the type definition? For unique_ptr usage, I use a deleter class, that handles deletion of individual types (limiting it to just two types, for brevity): struct SDL

No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang

本小妞迷上赌 提交于 2019-12-18 05:18:12
问题 I'm catching a compile error when attempting to use unique_ptr on Apple platforms with -std=c++11 : $ make c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra -pipe -c 3way.cpp In file included ... ./smartptr.h:23:27: error: no type named 'unique_ptr' in namespace 'std' using auto_ptr = std::unique_ptr<T>; ~~~~~^ ./smartptr.h:23:37: error: expected ';' after alias declaration using auto_ptr = std::unique_ptr<T>; According to Marshall Clow, who I consider an expert on the C++

Inserting a vector of unique_ptr into another vector

给你一囗甜甜゛ 提交于 2019-12-18 04:37:12
问题 I have a vector of unique_ptr's and I want to append them to another vector of unique_ptrs. I would normally do a simple insert: std::vector<std::unique_ptr<foo>> bar; bar.push_back(std::unique_ptr<foo>(new foo(1))); std::vector<std::unique_ptr<foo>> baz; baz.push_back(std::unique_ptr<foo>(new foo(2))); bar.insert(bar.end(), baz.begin(), baz.end()); However this gives me compile errors similar to this: /usr/include/c++/4.8/bits/stl_algobase.h:335: error: use of deleted function 'std::unique

Using unique_ptr to control a file descriptor

与世无争的帅哥 提交于 2019-12-17 19:23:02
问题 In theory, I should be able to use a custom pointer type and deleter in order to have unique_ptr manage an object that is not a pointer. I tried the following code: #ifndef UNIQUE_FD_H #define UNIQUE_FD_H #include <memory> #include <unistd.h> struct unique_fd_deleter { typedef int pointer; // Internal type is a pointer void operator()( int fd ) { close(fd); } }; typedef std::unique_ptr<int, unique_fd_deleter> unique_fd; #endif // UNIQUE_FD_H This doesn't work (gcc 4.7 with the -std=c++11

Bad practice to return unique_ptr for raw pointer like ownership semantics?

*爱你&永不变心* 提交于 2019-12-17 17:59:44
问题 I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a unique_ptr . class Foobar { public: static unique_ptr<Foobar> factory(DataObject data); } My intent is to tell client code that they own the pointer. Without a smart pointer, I would simply return Foobar* . I would like, however, to enforce that

How to declare std::unique_ptr and what is the use of it?

南楼画角 提交于 2019-12-17 17:34:01
问题 I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example: #include <utility> //declarations of unique_ptr using std::unique_ptr; // default construction unique_ptr<int> up; //creates an empty object // initialize with an argument unique_ptr<int> uptr (new int(3)); double *pd= new double; unique_ptr<double> uptr2 (pd); // overloaded * and -> *uptr2 = 23.5; unique_ptr<std::string> ups (new std::string("hello")); int len=ups-

Why is `make_unique<T[N]>` disallowed?

有些话、适合烂在心里 提交于 2019-12-17 15:54:34
问题 Assume namespace std throughout. The C++14 committee draft N3690 defines std::make_unique thus: [n3690: 20.9.1.4]: unique_ptr creation [unique.ptr.create] template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args); 1 Remarks: This function shall not participate in overload resolution unless T is not an array. 2 Returns: unique_ptr<T>(new T(std::forward<Args>(args)...)). template <class T> unique_ptr<T> make_unique(size_t n); 3 Remarks: This function shall not participate in

unique_ptr and OpenSSL's STACK_OF(X509)*

拜拜、爱过 提交于 2019-12-17 09:58:20
问题 I use some using statements and unique_ptr to work with OpenSSL, as suggested in another question. Without, code becomes really ugly and I am not so much a fan of goto statements. So far I have changed my code as far as possible. Here are examples, what I use: using BIO_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>; using X509_ptr = std::unique_ptr<X509, decltype(&::X509_free)>; using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; using PKCS7_ptr = std::unique_ptr

C++ inserting unique_ptr in map

随声附和 提交于 2019-12-17 09:43:53
问题 I have a C++ object of type ObjectArray typedef map<int64_t, std::unique_ptr<Class1>> ObjectArray; What is the syntax to create a unique_ptr to a new object of type Class1 and insert it into an object of type ObjectArray ? 回答1: As a first remark, I wouldn't call it ObjectArray if it is a map and not an array. Anyway, you can insert objects this way: ObjectArray myMap; myMap.insert(std::make_pair(0, std::unique_ptr<Class1>(new Class1()))); Or this way: ObjectArray myMap; myMap[0] = std::unique