smart-pointers

Non-ownership copies of std::unique_ptr

╄→尐↘猪︶ㄣ 提交于 2021-02-05 08:58:06
问题 There's two containers: owner and non-owner of resource. As I have only 1 owner, I suppose I need unique_ptr. class OwnershipContainer { public: void add(std::unique_ptr<Obj> obj) { objects.push_back(std::move(obj)); } Obj* get(std::size_t i) { return objects[i].get(); } private: std::vector<std::unique_ptr<Obj>> objects; }; What of kind of pointer i have to use for non-owner container? First thought was raw pointer. But i cannot give guarantee that lifetime of Obj match or exceed lifetime of

std::unique_ptr with std::map

谁都会走 提交于 2021-01-28 07:27:25
问题 I have a std::map where the key is std::shared_ptr<Foo> and the value is std::unique_ptr<Bar> where Foo and Bar are very different classes from a third-party library. I am using this std::map object as an in-memory cache. I am wondering what the best way of inserting a new entry into this map will be and then returned from a method, given that the Bar passed into the std::unique_ptr will already be constructed? I currently have the following: class SomeClass { public: const Bar*

std::shared_ptr which is empty but not null

无人久伴 提交于 2021-01-27 22:08:38
问题 http://www.cplusplus.com/reference/memory/shared_ptr/ says A shared_ptr that does not own any pointer is called an empty shared_ptr. A shared_ptr that points to no object is called a null shared_ptr and shall not be dereferenced. Notice though that an empty shared_ptr is not necessarily a null shared_ptr, and a null shared_ptr is not necessarily an empty shared_ptr. Am I able to create an empty std::shared_ptr, i.e. one which does not own anything but which is not null, i.e. contains payload?

When a unique_ptr is deallocated?

那年仲夏 提交于 2021-01-27 16:11:58
问题 In this code: void f(std::unique_ptr<int> q) { } void g() { std::unique_ptr<int> p{new int{42}}; f(std::move(p)); } At which line p is deallocated? I'd say at the exit of the f function because it was moved there using std::move, but I'm not sure nor confident about this answer. 回答1: At which line p is deallocated? At the end of the scope where it was declared i.e the function g in this case. That is when objects with automatic storage are destroyed, and their memory deallocated. The integer

Borrowed RefCell does not last long enough when iterating over a list

折月煮酒 提交于 2021-01-27 12:30:13
问题 I'm trying to implement a linked list to understand smart pointers in Rust. I defined a Node : use std::{cell::RefCell, rc::Rc}; struct Node { val: i32, next: Option<Rc<RefCell<Node>>>, } and iterate like fn iterate(node: Option<&Rc<RefCell<Node>>>) -> Vec<i32> { let mut p = node; let mut result = vec![]; loop { if p.is_none() { break; } result.push(p.as_ref().unwrap().borrow().val); p = p.as_ref().unwrap().borrow().next.as_ref(); } result } the compiler reports an error: error[E0716]:

Issue passing mutable Arc reference to hyper service_fn handler

蹲街弑〆低调 提交于 2021-01-24 07:09:21
问题 I've been trying the following Relevant imports and code shown use std::sync::{Arc, Mutex}; use std::thread; use hyper::rt::{self, Future, Stream}; use hyper::service::service_fn; use hyper::{Body, Request, Response, Server, StatusCode}; pub struct ChallengeState; pub struct ChallengeResponse; type BoxFut<'a> = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send + 'a>; fn handle_challengeproof<'a>( req: Request<Body>, challenge: &Arc<Mutex<ChallengeState>>, ) -> BoxFut<'a> { let