smart-pointers

Why is unique_ptr operator-> not const-overloaded?

蓝咒 提交于 2019-12-05 00:20:05
std::unique_ptr::operator-> has the signature pointer operator->() const noexcept; So operator-> is const but returns a mutable pointer. This allows for code like: void myConstMemberFunction() const { myUniquePtrMember->nonConstFunction(); } Why does the standard allow this, and what is the best way to prevent usage as presented above? Think about it like a normal pointer: int * const i; is a const pointer to a non- const int . You can change the int , but not the pointer. int const * i; is a non- const pointer to a const int . You can change the pointer but not the int . Now, for unique_ptr ,

Pimpl idiom using shared_ptr working with incomplete types

风格不统一 提交于 2019-12-04 23:49:26
问题 I'm reading Effective Modern C++ by Scott Meyers and he's discussing the use of the pimpl idiom and pointing to the implementation class with unique_ptr , but there is an issue of special member functions (such as destructors) requiring the type to be complete. This is because unique_ptr 's default deleter statically asserts whether the type to be deleted is complete, before delete p is used. So any special member functions of the class must be defined in the implementation file (rather than

Pointer-like classes and the ->* operator

血红的双手。 提交于 2019-12-04 23:33:42
I've recently come across the need to apply a pointer-to-member to the object designated by an iterator. I've tried the natural syntax : ite->*ptr = 42; To my dismay, it didn't compile. Iterators don't overload operator->* , but more surprisingly neither do smart pointers. I needed to resort to the following clunkiness : (*ite).*ptr = 42; Experimenting (see the live example below) has shown that such a syntax seems to be achievable for custom classes, for both pointers-to-members and pointers-to-member-functions, at least since C++14. Thus : Is there a reason the standard pointer-like classes

Why can't a pointer be automatically converted into a unique_ptr when returning it?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:13:52
Let me pose my question through an example. #include <memory> std::unique_ptr<int> get_it() { auto p = new int; return p; } int main() { auto up ( get_it() ); return 0; } This fails to compile with the following error: a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’ return p; ^ Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead? Motivation: I understand it's supposed to be good practice to use smart pointers for ownership to be clear; I'm getting a pointer (which I own) from somewhere, as an int* in

How to make_shared a derived class?

放肆的年华 提交于 2019-12-04 22:58:01
I want to use the make_shared<T> function with a derived class, like below class Base { public: typedef std::shared_ptr<Base> Ptr; }; class Derived : public Base {}; Base::Ptr myPtr = std::make_shared(/* Derived() */ ); How can I tell make_shared to build such an object? I want to avoid the classical Base::Ptr ptr = Base::Ptr(new Derived()); To make use of the single alloc in the make_shared function. std::shared_ptr has a converting constructor that can make a shared_ptr<Base> from a shared_ptr<Derived> , so the following should work: #include <memory> class Base { public: typedef std::shared

Why is there no boost::copy_on_write_ptr?

末鹿安然 提交于 2019-12-04 22:20:50
问题 I just saw this nice copy-on-write pointer implementation. It looks pretty generic and useful, so my question is: Is such a class contained in any of the C++ toolkits (boost, loki, etc.)? If not, I'd really like to know why because it is a really useful idiom and apparently a generic implementation seems doable (like the one I linked to). 回答1: There was a lot of debate over the possibility, and at least one suggested version of what eventually came out as auto_ptr was for a reference counted

shared_ptr - pass by value vs pass by reference

霸气de小男生 提交于 2019-12-04 20:04:11
问题 Suppose I have: typedef boost::shared_ptr<Event> EventPtr; On one thread, I am creating an Event and sending it off to get dispatched: Event* event = new Event(); EventPtr eventPtr(event); EventDispatcher::dispatch(eventPtr); //pseudocode The EventDispatcher receives an EventPtr and adds it to a queue that gets processed in another thread...but what is an appropriate method signature for the dispatch method? dispatch(EventPtr event); //will push_back(event) or dispatch(const EventPtr& event);

using custom deleter with unique_ptr

余生颓废 提交于 2019-12-04 18:21:04
问题 With shared_ptr you can use a custom deleter, like: auto fp = shared_ptr<FILE>( fopen("file.txt", "rt"), &fclose ); fprintf( fp.get(), "hello\n" ); and this will remember to fclose the file regardless of how the function exits. However, it seems a bit overkill to refcount a local variable, so I want to use unique_ptr : auto fp = unique_ptr<FILE>( fopen("file.txt", "rt"), &fclose ); however, that does not compile. Is this a defect? Is there a simple workaround? Im I missing something trivial?

STL class for reference-counted pointers?

↘锁芯ラ 提交于 2019-12-04 17:17:06
问题 This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://archive.gamedev.net/reference/articles/article1060.asp 回答1: With the exception of the already mentionned TR1 shared_ptr, there is no reference-counted pointer in STL. I suggest you use boost::shared_ptr (downloading boost will

Dynamic Pointer Cast

放肆的年华 提交于 2019-12-04 16:58:49
I'd like to convert a base class pointer to a derived class pointer as a function argument without using dynamic_pointer_cast class Base { public: typedef std::shared_ptr < Base > Ptr; virtual ~Base ( ); ... }; class Derive : public Base { public: typedef std::shared_ptr < Derive > Ptr; ... }; void foo( Derive::Ptr ptr ) { ... } Base::Ptr ptr1 ( new Derive ); foo( ptr1 ); The above code will give an error while calling foo. This can be avoided by typecasting ptr1 into a Dervie pointer using std::dynamic_pointer_cast. Base::Ptr ptr1 ( new Derive ); foo ( std::dynamic_pointer_cast < Derive > (