make-shared

std::make_shared() change in C++17

我们两清 提交于 2019-12-20 16:29:10
问题 In cppref, the following holds until C++17: code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. 回答1: The evaluation order of function arguments are changed by P0400R0. Before the change, evaluation of function arguments are

how to create boost phoenix make_shared?

狂风中的少年 提交于 2019-12-19 08:14:24
问题 Is it possible to create boost phoenix lazy variant of std::make_shared ? I mean, to make possible something like namespace p = boost::phoenix; ... expr = custom_parser[_a=p::make_shared<Node>(_1,_2,_3)] >> ... One cannot use BOOST_PHOENIX_ADAPT_FUNCTION because of variadic template nature of std::make_shared . So, probably wrapper should be variadic template itself, if it is possible to write one. 回答1: If you can spare an extra set of parentheses: namespace { template <typename T> struct

How to pass deleter to make_shared?

允我心安 提交于 2019-12-18 10:48:08
问题 Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int>

was raw-pointer constructor of shared_ptr a mistake?

牧云@^-^@ 提交于 2019-12-12 07:33:32
问题 In hindsight, given make_shared , would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11? Are there strong arguments or use cases in favor of this constructor? It would have avoided the well documented pitfall of exception-safety and the memory allocation/performance advantage of using make_shared . I believe another advantage of requiring shared_ptr construction via make_shared would be that it could be a single pointer under the hood, lowering its

Trouble constructing shared_ptr

此生再无相见时 提交于 2019-12-11 04:58:48
问题 I'm new to smart pointers, and I'm in the process of hitting every stumbling block. I have a struct texture_t : struct texture_t { hash32_t hash; uint32_t width; uint32_t height; uint32_t handle; }; When I try and make a shared_ptr of this struct using this line: auto texture_shared_ptr = std::make_shared<texture_t>(new texture_t()); I get this error: error C2664: 'mandala::texture_t::texture_t(const mandala::texture_t &)' : cannot convert parameter 1 from 'mandala::texture_t *' to 'const

new and make_shared for shared pointers

别等时光非礼了梦想. 提交于 2019-12-09 18:14:04
问题 I came across this post and one of the answers by @kerek SB states std::shared_ptr<Object> p1 = std::make_shared<Object>("foo"); std::shared_ptr<Object> p2(new Object("foo")); In your code, the second variable is just a naked pointer, not a shared pointer at all. Now on the meat. make_shared is (in practice) more efficient, because it allocates the reference control block together with the actual object in one single dynamic allocation. By contrast, the constructor for shared_ptr that takes a

boost::make_shared is not calling (placement) operator new?

不羁的心 提交于 2019-12-07 03:09:49
问题 I am using boost::make_shared for the first time to create objects pointed to by shared pointers. Mainly because our code was too slow and the single allocation really helped to improve performance. After fixing some memory leaks "the hard manual way" I decided to implement a simple memory leak detector by overriding new operators for all relevant classes just for counting which objects are still alive at specific points in our application. I have implemented this several times before and was

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

空扰寡人 提交于 2019-12-05 13:00:44
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 standard for some other reason? Think about how the std::shared_ptr constructor works: std::shared_ptr

Does std::make_shared() use custom allocators?

大兔子大兔子 提交于 2019-12-04 16:09:42
问题 Consider this code: #include <memory> #include <iostream> class SomeClass { public: SomeClass() { std::cout << "SomeClass()" << std::endl; } ~SomeClass() { std::cout << "~SomeClass()" << std::endl; } void* operator new(std::size_t size) { std::cout << "Custom new" << std::endl; return ::operator new(size); } void operator delete(void* ptr, std::size_t size) { std::cout << "Custom delete" << std::endl; ::operator delete(ptr); } }; int main() { std::shared_ptr<SomeClass> ptr1(new SomeClass);

How to make boost::make_shared a friend of my class

不打扰是莪最后的温柔 提交于 2019-12-04 08:11:01
问题 I have written a class with protected constructor, so that new instances can only be produced with a static create() function which returns shared_ptr's to my class. To provide efficient allocation I'd like to use boost::make_shared inside the create function, however the compiler complains that my class constructor is protected inside boost::make_shared. I decided to my boost::make_shared a friend of my class but I'm puzzled about the syntax. I tried template< class T, class A1, class A2 >