make-shared

how to block usage of std::make_shared<T>

橙三吉。 提交于 2021-02-07 21:54:46
问题 I know I can prevent ordinary heap allocation of custom class and its descendants by making the class's operator new private, but is there any way to prevent a user of a library from calling std::make_shared on a custom class (or its descendants)? Apparently, simply making the operator new private in a class does not stop it. Note that I do not want to completely prevent shared pointers from being created by any means, as I intend to still be able to produce a std::shared_ptr for my custom

C++: How to partially deduce template arguments from make_shared

拟墨画扇 提交于 2020-12-12 02:53:40
问题 In order to circumvent the restriction on partially supplied explicit template arguments, I embed the struct from which I want to deduce the class template parameters ( Internal ) into a second struct ( Container ). I would like to enable the user of the code to create e.g. shared pointers of the resulting type. By writing my own create function within the struct, this works just fine. #include <memory> /// Container that is used in order to partially specify template arguments template <int

C++: How to partially deduce template arguments from make_shared

孤人 提交于 2020-12-12 02:53:16
问题 In order to circumvent the restriction on partially supplied explicit template arguments, I embed the struct from which I want to deduce the class template parameters ( Internal ) into a second struct ( Container ). I would like to enable the user of the code to create e.g. shared pointers of the resulting type. By writing my own create function within the struct, this works just fine. #include <memory> /// Container that is used in order to partially specify template arguments template <int

C++11 Passing 'this' as paramenter for std::make_shared

。_饼干妹妹 提交于 2020-07-18 04:17:22
问题 I'm trying to pass 'this' to the constructor using std::make_shared Example: // headers class A { public: std::shared_ptr<B> createB(); } class B { private: std::shared_ptr<A> a; public: B(std::shared_ptr<A>); } // source std::shared_ptr<B> A::createB() { auto b = std::make_shared<B>(this); // Compiler error (VS11 Beta) auto b = std::make_shared<B>(std::shared_ptr<A>(this)); // No compiler error, but doenst work return b; } However this does not work properly, any suggestions how I can

How do I pass protobuf's boost::shared_ptr pointer to function?

邮差的信 提交于 2020-01-24 17:07:10
问题 I have to pass a boost::shared_ptr : boost::shared_ptr<Protobuf::Person::Profile> pProfile = boost::make_shared<Protobuf::Person::Profile>(); which is protobuf's pointer, to a protobuf's function oPerson.set_allocated_profile(pProfile) but oPerson.set_allocated() expects a pointer to Protobuf::Person::Profile . I have tried couple of ways but I think when I try to convert protobuf object to JSON using pbjson::pb2Json which is a library function built on rapid json, the pointer goes out of

How do I pass protobuf's boost::shared_ptr pointer to function?

馋奶兔 提交于 2020-01-24 17:06:26
问题 I have to pass a boost::shared_ptr : boost::shared_ptr<Protobuf::Person::Profile> pProfile = boost::make_shared<Protobuf::Person::Profile>(); which is protobuf's pointer, to a protobuf's function oPerson.set_allocated_profile(pProfile) but oPerson.set_allocated() expects a pointer to Protobuf::Person::Profile . I have tried couple of ways but I think when I try to convert protobuf object to JSON using pbjson::pb2Json which is a library function built on rapid json, the pointer goes out of

Why must shared_ptr<> allocate for the control block and managed object separately?

こ雲淡風輕ζ 提交于 2020-01-13 12:16:24
问题 This linked question asked if the make_shared<> function and the shared_ptr<> constructor differ. What happens when using make_shared Part of the answer was that make_shared<> will usually allocate memory for both the pointed to object and smart pointer control block in a single allocation. The shared_ptr<> constructors use two allocations. cppreference states that the constructors "must" do so but no reason is given. Why is this? Is it for some reason impossible? Or is it forbidden by the

Debuggable replacement for make_shared()

﹥>﹥吖頭↗ 提交于 2019-12-23 18:05:36
问题 Using gcc 4.6.2, make_shared() gives a useless backtrace (apparently due to some rethrow) if a constructor throws an exception. I'm using make_shared() to save a bit of typing, but this is show stopper. I've created a substitute make_shrd() that allows a normal backtrace. I'm using gdb 7.3.1. I'm worried that: The bad backtrace under make_shared() is somehow my own fault My substitute make_shrd() will cause me subtle problems. Here's a demo: #include <memory> #include <stdexcept> using

What does “single allocation” mean for boost::make_shared

回眸只為那壹抹淺笑 提交于 2019-12-21 09:09:09
问题 In the boost doc of make_shared, it says: 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 construction overhead. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr

What does “single allocation” mean for boost::make_shared

拜拜、爱过 提交于 2019-12-21 09:08:29
问题 In the boost doc of make_shared, it says: 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 construction overhead. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr