shared-ptr

__nat class in clang standard libarry

喜欢而已 提交于 2019-12-08 02:03:51
问题 I was looking through clang's c++ standard library, and found this class in the shared_ptr class. class shared_ptr ... private: struct __nat {int __for_bool_;}; ... }; and I understand that this class is used to detect whether type conversion is possible at compile time, but its member __for_bool_ is never used anywhere in the class or the weak_ptr counterpart. So my question is, what is the point of __for_bool_ ? Why not simply use an empty class for the same purpose? I'm sure the standard

Why does enable_shared_from_this lack direct access to the embedded weak_ptr?

喜你入骨 提交于 2019-12-08 01:45:06
问题 I want to use boost signals2 with automatic connection management in a multithreaded application. My class inherits from enable_shared_from_this<> and i want to connect a member method from within another member method. The connection might be rebuilt frequently so my code should be as fast as possible (despite from the boost signals2 performance itself): typedef boost::signals2::signal<void ()> signal_type; struct Cat : public enable_shared_from_this<Cat> { void meow (); void connect (signal

C++NRVO guarantees? Or better prefer non-const ref param or shared_ptr?

主宰稳场 提交于 2019-12-08 00:41:15
问题 I have been using C++ since 1992 (and reading copious amounts about the language), so I know a fair amount about the language, but far from all. My question is about C++11 named return value optimization - what guarantees are there that it will be performed? I tend to prefer to send in non-const parameters (C++97 style) or use shared_ptr (C++11 style), or even used ptr-to-ptr (C style). One reason is that with non-const ref args or shared_ptr, I am guaranteed that NO extra object copies are

Using mem_fun_ref with boost::shared_ptr

醉酒当歌 提交于 2019-12-07 20:56:10
问题 Following the advice of this page, I'm trying to get shared_ptr to call IUnknown::Release() instead of delete: IDirectDrawSurface* dds; ... //Allocate dds return shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(&IUnknown::Release)); error C2784: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result (__thiscall _Ty::* )(_Arg) const' from 'ULONG (__cdecl IUnknown::* )(void)' error C2784: 'std:

dynamic cast of a shared_ptr

混江龙づ霸主 提交于 2019-12-07 17:42:15
问题 I have a few classes of which I've made std::shared_ptr versions, as follows: typedef std::shared_ptr<MediaItem> MediaItemPtr; typedef std::shared_ptr<ImageMediaItem> ImageMediaItemPtr; class MediaItem { //stuff here } class ImageMediaItem : public MediaItem { //more stuff here } Internally, I pass everything around as a MediaItemPtr object, but when I try to cast to a ImageMediaItemPtr, nothing I try seems to work. For example: ImageMediaItemPtr item = std::dynamic_pointer_cast

C++/CLI Wrapping a Function that Returns a std::shared_ptr

坚强是说给别人听的谎言 提交于 2019-12-07 11:55:19
问题 I'm currently wrapping a C++ class with C++/CLI for .NET interoperability following the standard process of holding a native pointer in a managed class. In one instance, I have a native class that has a function like: std::shared_ptr<BaseChannel> channelData(const int RunNumber); I have already begun creating a wrapper class for BaseChannel . However, if I pass the raw pointer to the constructor of the managed class, there are no guarantees on the lifetime of the object being pointed to by

shared_ptr: what's it used for

半世苍凉 提交于 2019-12-07 11:41:54
问题 I make a lot of use of boost::scoped_ptr in my code and it is great but I'm currently working with software that uses shared_ptr all over the place and I'm wondering if I'm missing something. AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data and you don't know what order the threads are going to finish (with the shared_ptr ensuring that the object exists until the last thread has finished with it). Are there other use cases? 回答1: Threads are

friend function of std::make_shared() in Visual Studio 2010 (not Boost)

大憨熊 提交于 2019-12-07 06:58:23
问题 how to make friend function of std::make_shared() . I tried: class MyClass{ public: friend std::shared_ptr<MyClass> std::make_shared<MyClass>(); //or //friend std::shared_ptr<MyClass> std::make_shared(); protected: MyClass(); }; but it does not work (i'am using Visual Studio 2010 SP1) 回答1: How about adding a static method to your class: class Foo { public: static shared_ptr<Foo> create() { return std::shared_ptr<Foo>(new Foo); } private: // ... }; Here's a little hackaround: class Foo {

bad_weak_ptr when calling shared_from_this() in base class

ぐ巨炮叔叔 提交于 2019-12-07 05:35:58
问题 I have a SuperParent class, a Parent class (derived from SuperParent ) and both contain a shared_ptr to a Child class (which contains a weak_ptr to a SuperParent ). Unfortunately, I'm getting a bad_weak_ptr exception when trying to set the Child 's pointer. The code is the following: #include <boost/enable_shared_from_this.hpp> #include <boost/make_shared.hpp> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> using namespace boost; class SuperParent; class Child { public: void

Can I cast shared_ptr<T> & to shared_ptr<T const> & without changing use_count?

荒凉一梦 提交于 2019-12-07 04:51:16
问题 I have a program that uses boost::shared_ptr s and, in particular, relies on the accuracy of the use_count to perform optimizations. For instance, imagine an addition operation with two argument pointers called lhs and rhs. Say they both have the type shared_ptr<Node> . When it comes time to perform the addition, I'll check the use_count , and if I find that one of the arguments has a reference count of exactly one, then I'll reuse it to perform the operation in place. If neither argument can