smart-pointers

Does C++11 have wrappers for dynamically-allocated arrays like Boost's scoped_array?

故事扮演 提交于 2019-11-28 16:52:33
I often need to deal with dynamically-allocated arrays in C++, and hence rely on Boost for scoped_array, shared_array, and the like. After reading through Stroustrup's C++11 FAQ and the C++11 Reference Wiki , I could not find a suitable replacement for these dynamic array wrappers that is provided by the C++11 standard. Is there something that I have overlooked, or do I have to continue relying on Boost? Thank you very much for your help! There is a specialization of unique_ptr , like unique_ptr<T[]> . #include <iostream> #include <memory> struct test { ~test() { std::cout << "test::dtor" <<

C++ object-pool that provides items as smart-pointers that are returned to pool upon deletion

醉酒当歌 提交于 2019-11-28 15:52:08
问题 I'm having fun with c++-ideas, and got a little stuck with this problem. I would like a LIFO class that manages a pool of resources. When a resource is requested (through acquire() ), it returns the object as a unique_ptr that, upon deletion, causes the resource to be returned to the pool. The unit tests would be: // Create the pool, that holds (for simplicity, int objects) SharedPool<int> pool; TS_ASSERT(pool.empty()); // Add an object to the pool, which is now, no longer empty pool.add(std:

shared_ptr vs scoped_ptr

早过忘川 提交于 2019-11-28 15:47:46
问题 scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr . So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptr is better (or recommended) to use instead of scoped_ptr . Does scoped_ptr work much faster from shared_ptr , or

What is the best smart pointer return type for a factory function?

跟風遠走 提交于 2019-11-28 15:38:11
问题 With respect to smart pointers and new C++11/14 features, I am wondering what the best-practice return values and function parameter types would be for classes that have these facilities: A factory function (outside of the class) that creates objects and returns them to users of the class. (For example opening a document and returning an object that can be used to access the content.) Utility functions that accept objects from the factory functions, use them, but do not take ownership. (For

Why do C++ libraries and frameworks never use smart pointers?

家住魔仙堡 提交于 2019-11-28 14:42:38
问题 I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it's scoped or shared pointers. However, I noticed that frameworks like Qt, wxWidgets and libraries like Boost never return nor expect smart pointers, as if they were not using them at all. Instead, they return or expect raw pointers. Is there any reason for that? Should I stay away from smart pointers when I write a public API, and why? Just wondering

Boost weak_ptr's in a multi-threaded program to implement a resource pool

风流意气都作罢 提交于 2019-11-28 12:34:21
I'm thinking of using boost::weak_ptr to implement a pool of objects such that they will get reaped when nobody is using one of the objects. My concern, though, is that it's a multi-threaded environment, and it seems there's a race condition between the last shared_ptr to an object going out of scope and a new shared_ptr being constructed from the weak_ptr. Normally, you'd protect such operations with lock or something; however, the whole point here is that you don't know when the shared_ptr might be going out of scope. Am I misunderstanding something about boost::shared_ptr and boost::weak

enable_shared_from_this (c++0x): what am I doing wrong?

淺唱寂寞╮ 提交于 2019-11-28 12:07:49
I'm just toying around with the smart pointers in the upcoming new c++ standard. However I fail to grasp the usage of the shared_from_this function. Here is what I have: #include <iostream> #include <memory> class CVerboseBornAndDie2 : public std::enable_shared_from_this<CVerboseBornAndDie2> { public: std::string m_Name; CVerboseBornAndDie2(std::string name) : m_Name(name) { std::cout << m_Name << " (" << this << ") is born!" << std::endl; } virtual ~CVerboseBornAndDie2() { std::cout << m_Name << " (" << this << ") is dying!" << std::endl; } }; int main(){ CVerboseBornAndDie2* vbad = new

How to check memory allocation failures with new operator?

谁说我不能喝 提交于 2019-11-28 10:42:41
Just recently I switched the language of my project to use C++ from C. With C, I used malloc and after that I check if malloc was successful but with C++, I use 'new' to allocate memory and I would like to know how you would normally check the memory allocation failure. From my google search, I saw nothrow like the following. char *buf = new (nothrow)char[10]; I also saw the following. try{} catch(bad_alloc&) {} But what about the following? I am using some of chrome library routines to use smart pointers. For instance, I have the code as follows. scoped_array<char> buf(new char[MAX_BUF]); It

How to uniform initialize map of unique_ptr?

陌路散爱 提交于 2019-11-28 07:34:52
问题 I have this code to initialize map from into to unique_ptr. auto a = unique_ptr<A>(new A()); map<int, unique_ptr<A>> m; m[1] = move(a); Can I use uniform initialize this? I tried map<int, unique_ptr<A>> m {{1, unique_ptr<A>(new A())}}; But I got an error. Some part of error message is In instantiation of 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<A, std::default_delete<A> > >&}; _Val = std::pair<const int, std::unique_ptr<A>

intrusive_ptr in c++11

你说的曾经没有我的故事 提交于 2019-11-28 07:14:14
Does C++11 have something equivalent to boost::intrusive_ptr ? My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use std::shared_ptr because I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something like boost::intrusive_ptr . Does c++11 have something equivalent to boost::intrusive_ptr? No. It does have std::make_shared which means std::shared_ptr is almost (see note below) as efficient as an intrusive