make-shared Questions

4

Solved

Here's my code. When compiling I get the error invalid declarator before ‘geometry’ at line 16 and line 48, I am not sure what I am doing wrong. Please advise. #include <iostream> #inc...

4

Solved

buffer = new char[64]; buffer = std::make_shared<char>(char[64]); ??? Can you allocate memory to an array using make_shared<>()? I could do: buffer = std::make_shared<char>( ne...
Lancey asked 10/12, 2012 at 3:5

1

Solved

I am missing something with std::make_shared. Can't it resolve the type of a std::initializer_list, or am I doing something else wrong? #include <vector> #include <memory> class A {};...
Dentation asked 6/4, 2020 at 11:40

3

When using std::make_shared<C> the class overloaded new/delete operators are not called. When using std::shared_ptr<C>, std::unique_ptr<C> & std::make_unique<C> the cl...
Jaenicke asked 13/9, 2019 at 19:8

5

Solved

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 oper...
Manaker asked 12/12, 2015 at 18:23

1

Solved

as of C++17 you can use make_unique in order to create smart pointers to arrays, such as: unique_ptr<int[]> ptr = make_unique<int[]>(10); which will create a smart pointer to an arra...
Annetteannex asked 30/8, 2018 at 10:59

2

Solved

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, whi...
Vermiculation asked 17/2, 2018 at 14:33

1

Solved

As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from...
Disorderly asked 15/7, 2016 at 13:19

3

Solved

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...
Pashalik asked 22/5, 2016 at 17:45

6

Solved

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...
Attic asked 31/7, 2010 at 14:31

2

Solved

I'm a relative C++ newbie trying to convert an existing project from raw pointers, with a convoluted memory-management protocol, over to using C++11's shared_ptr. Overall it is going very smoothly,...
Markland asked 23/9, 2015 at 16:42

2

Solved

A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is ke...
Castera asked 20/8, 2015 at 8:45

4

Solved

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")); ...
Melodie asked 22/11, 2014 at 21:39

1

Solved

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<> w...
Conveyancing asked 14/10, 2014 at 2:48

3

Solved

I'm interested if these two lines of code are the same: shared_ptr<int> sp(new int(1)); // double allocation? shared_ptr<int> sp(make_shared<int>(1)); // just one allocation? I...
Mathre asked 16/7, 2014 at 11:42

2

Solved

My question concerns shared_ptr and make_shared in C++11. I have two vectors, the first one stores smart pointers and the second one stores raw pointers. The first vector works as I had expepted bu...
Lovable asked 7/5, 2014 at 21:54

2

Solved

C++'s new has an option to return a null pointer instead of throwing a bad_alloc exception when an allocation failed. Foo * pf = new(std::nothrow) Foo(1, 2, 3); (Yes, I understand this only prev...
Shriner asked 19/3, 2014 at 15:49

2

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&gt...
Contemporaneous asked 2/2, 2014 at 21:2

3

Solved

Consider the following: class DirectoryIterator; namespace detail { class FileDataProxy; class DirectoryIteratorImpl { friend class DirectoryIterator; friend class FileDataProxy; WIN32_FI...
Uninhibited asked 7/4, 2010 at 6:0

2

Solved

Consider this code: #include <memory> #include <iostream> class SomeClass { public: SomeClass() { std::cout << "SomeClass()" << std::endl; } ~SomeClass() { std::cou...
Yadirayaeger asked 29/12, 2012 at 9:35

2

Solved

Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory - to implement a...
Macleod asked 22/8, 2010 at 13:29

3

Solved

Boost's make_shared() function promises to be exception-safe while attempting to create a shared_ptr. Why is there no make_scoped() equivalent? Is there a common best practice? Here's a code exam...
Quinquennial asked 5/7, 2012 at 2:46

1

Solved

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...
Pitzer asked 10/5, 2012 at 12:40

2

Solved

When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherCla...
Monotheism asked 23/4, 2012 at 23:40

2

Solved

In the absence of variadic templates (still!) in Visual Studio 2010/2011, a constructor that takes a lot of parameters can be problematic. For example the following won't compile: MyMaterials.pus...
Cudgel asked 4/4, 2012 at 13:41

© 2022 - 2024 — McMap. All rights reserved.