smart-pointers

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

Can Google Mock a method with a smart pointer return type?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 06:27:35
问题 I have a factory that returns a smart pointer. Regardless of what smart pointer I use, I can't get Google Mock to mock the factory method. The mock object is the implementation of a pure abstract interface where all methods are virtual. I have a prototype: MOCK_METHOD0(Create, std::unique_ptr<IMyObjectThing>()); And I get: "...gmock/gmock-spec-builders.h(1314): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'" The type

Differences between std::make_unique and std::unique_ptr with new

回眸只為那壹抹淺笑 提交于 2019-12-16 22:45:08
问题 Does std::make_unique have any efficiency benefits like std::make_shared ? Compared to manually constructing std::unique_ptr : std::make_unique<int>(1); // vs std::unique_ptr<int>(new int(1)); 回答1: The motivation behind make_unique is primarily two-fold: make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries. foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique

Convert container of pointers to smart pointers?

不羁的心 提交于 2019-12-14 00:33:36
问题 Is there a concise, generic way to convert a std container (such as vector ) of regular/dumb pointers: vector< T* > to, for instance, boost::shared_ptr ?: vector< boost::shared_ptr<T> > I thought I could pull it off using vector 's range constructor: vector< T* > vec_a; ... vector< boost::shared_ptr<T> > vec_b( vec_a.begin(), vec_a.end() ); but that refused to compile (Visual Studio 2008). EDIT: Test code: void test() { vector< int* > vec_a; vector< boost::shared_ptr<int> > vec_b( vec_a.begin

Cyclic reference of RefCell borrows in traversal

余生颓废 提交于 2019-12-13 16:07:30
问题 I'm learning Rust and tried coding a doubly-linked list. However, I'm stuck already at a typical iterative traversal implementation. I'm getting the impression that the borrow checker / drop checker is too strict and cannot infer the correct lifetime for the borrow when it crosses the function boundary from RefCell . I need to repeatedly set a variable binding ( curr in this case) to the borrow of its current contents: use std::cell::RefCell; use std::rc::Rc; pub struct LinkedList<T> { head:

Do I have to call Release() method on CComPtr objects?

女生的网名这么多〃 提交于 2019-12-13 15:15:40
问题 I'm working with SAPI5 API for processing text to speech. If I simplify my code looks like this (I removed error checking to simplify it as much as possible): int main() { CoInitialize(NULL); CComPtr<ISpVoice> spVoice; spVoice.CoCreateInstance(CLSID_SpVoice); ... CoUninitialize(); return 0; } For some weird reason my code crashes if I don't call spVoice.Release(). So the code above crashes, but this code works nicely: int main() { CoInitialize(NULL); CComPtr<ISpVoice> spVoice; spVoice

boost::shared_ptr cycle break with weak_ptr

旧城冷巷雨未停 提交于 2019-12-13 12:38:48
问题 I am currently in a situation like: struct A { shared_ptr<B> b; }; struct B { shared_ptr<A> a; }; //... shared_ptr<A> a(new A()); shared_ptr<B> b(new B()); a->b(b); b->a(a); I know this won't work, because the references would continue to point to each other. I've also been told that weak_ptr solves this issue. However, weak_ptr has no get or -> overload. I've heard mentions of 'use lock() ', but can anyone give code examples of how to do this correctly? 回答1: Come on now. http://boost.org/doc

Update a smart pointer using a reference

人盡茶涼 提交于 2019-12-13 08:45:47
问题 I would like to update a smart pointer from a reference. shared_ptr<My_Toy> my_toy_ptr; // Something... void update(shared_ptr<My_Toy> my_toy_ptr, My_Toy& toy){ my_toy_ptr = &toy; } ...but his code generates an error. How can I do this operation? 回答1: Don't pass the address of a stack allocated object to a std::shared_ptr . toy will be destructed at the end of its scope and the std::shared_ptr will attempt to delete something that was not new d. The address held by a std::shared_ptr must be

What is the cost of calling member function via shared pointer?

跟風遠走 提交于 2019-12-13 05:11:11
问题 It is often stated that dereferencing a smart pointer does not have notable performance impacts. (For example here: C Smart Pointer Performance) I am now wondering if this is really true. I understand that it could be, if operations on the object referenced are atomic. Thinking of code based on this snippets: class Worker { [...] public: void setDataProvider( shared_ptr<DataProvider> p ) { m_provider = p; } void doWork() { [...] for(;;) { int d = m_provider->getSomeData(); [ do something with

Nonvirtual deleter with smart pointers

霸气de小男生 提交于 2019-12-13 04:56:35
问题 I was reading the latest Overload (link) and decided to test out the statement at page 8: shared_ptr will properly invoke B’s destructor on scope exit, even though the destructor of A is not virtual. I am using Visual Studio 2013, compiler v120: #include <memory> #include <iostream> struct A { ~A() { std::cout << "Deleting A"; } }; struct B : public A { ~B() { std::cout << "Deleting B"; } }; int main() { std::shared_ptr<A> ptr = std::make_shared<B>(); ptr.reset(); return 0; } This works as