shared-ptr

Unable to use custom allocator with allocate_shared/make_shared

邮差的信 提交于 2019-12-30 06:17:10
问题 In my C++11 program, I use shared_ptr<T> for some objects which are actively created and deleted. It so happened that standard allocator with operator new is a bottleneck, so I want to create my own one, which will allocate a bunch of memory at once and then give to to make_shared on demand. Unfortunatelly, this is the first time I write an allocator and I have no idea why GCC is unable to compile the following code: #include <memory> class MyAlloc { public: typedef char* pointer; typedef

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

此生再无相见时 提交于 2019-12-30 03:45:26
问题 OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a quirk of Clang or libc++ but I can't speak C++ standardese and I have no access to other compilers with C++11 support so I can't really check it, and I've searched the internet and Stack Overflow to the best of my ability without finding anything

About shared_ptr and pointer to member operator `->*` and `std::bind`

醉酒当歌 提交于 2019-12-29 06:17:31
问题 Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why

dynamic_cast across a shared_ptr?

亡梦爱人 提交于 2019-12-29 05:20:44
问题 I have two classes A and B, B inherits from A. If I have a shared_ptr<A> object which I know is really a B subtype, how can I perform a dynamic cast to access the API of B (bearing in mind my object is shared_ptr, not just A? 回答1: If you just want to call a function from B you can use one of these: std::shared_ptr<A> ap = ...; dynamic_cast<B&>(*ap).b_function(); if (B* bp = dynamic_cast<B*>(ap.get()) { ... } In case you actually want to get a std::shared_ptr<B> from the std::shared_ptr<A> ,

Shared void pointers. Why does this work?

余生长醉 提交于 2019-12-29 05:04:11
问题 To solve a very peculiar problem in my application I need a shared-pointer to allocated data, but to the outside world, the underlying data type should remain hidden. I could solve this by making some kind of Root class of which all my other classes inherit, and use a shared_ptr on this Root class, like this: std::shared_ptr<Root> However: I don't want all my classes to inherit from this Root class just to be able to have this shared pointer Sometimes I want to return a shared pointer to std:

C++ std::shared_ptr usage and information

﹥>﹥吖頭↗ 提交于 2019-12-29 04:36:32
问题 I am trying to use std::shared_ptr in my code. I have seen there have been other questions on the subject, but I am still getting a compiler error. Have I got the right version of gcc and setup? What I have done: I have tried to compile my code with both headers separately — <memory> and <tr1/memory> but still get the errors below in both cases. The version of gcc I am using is gcc --version gcc (GCC) 4.3.2 When I include <memory> header I use std::shared_ptr and with the <tr1/memory> header

C++ std::shared_ptr usage and information

耗尽温柔 提交于 2019-12-29 04:36:28
问题 I am trying to use std::shared_ptr in my code. I have seen there have been other questions on the subject, but I am still getting a compiler error. Have I got the right version of gcc and setup? What I have done: I have tried to compile my code with both headers separately — <memory> and <tr1/memory> but still get the errors below in both cases. The version of gcc I am using is gcc --version gcc (GCC) 4.3.2 When I include <memory> header I use std::shared_ptr and with the <tr1/memory> header

smart pointers + “this” considered harmful?

人走茶凉 提交于 2019-12-29 04:02:45
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

smart pointers + “this” considered harmful?

偶尔善良 提交于 2019-12-29 04:02:24
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

Are there any downsides with using make_shared to create a shared_ptr

天大地大妈咪最大 提交于 2019-12-29 02:45:08
问题 Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T) . Boost documentation states There have been repeated requests from users for a factory function that creates an object of a given type and returns a shared_ptr to it. Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block, eliminating a significant portion of shared_ptr's