Questions tagged [make-shared]

make_shared is a function for creating shared pointers introduced in C++11.

138 questions
3
votes
1 answer

no matching function for call to 'make_shared'

I am getting compiler error "no matching function for call to 'make_shared'" whenever I try to use a constructor that takes any arguments. So, for example: std::shared_ptr foo = std::make_shared (); works fine. But, std::shared_ptr
Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
3
votes
1 answer

std::bind a call to std::make_shared

I'm trying to create a functor that returns a shared_ptr by calling std::bind on std::make_shared, but the syntax is beyond me, or perhaps it's not even possible? Something like the following, assuming the constructor of MyBar takes a const…
tgoodhart
  • 3,111
  • 26
  • 37
3
votes
1 answer

GCC 8 fails to compile make_shared()

This code compiles cleanly and works with all compilers I've tried except for GCC 8 (and current GCC trunk): std::make_shared(0) I'd like to know: Is GCC 8 correct to refuse this code? Is there a substitute that GCC 8 will accept…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
3
votes
2 answers

Allocate char* array with boost::make_shared

To allocate a char* array I would normally write something like: char* arr = new char[size]; How can I achieve the same thing using boost::shared_ptr (or probably boost::shared_array) and boost::make_shared? My guesses are: 1)…
user2735714
  • 131
  • 7
3
votes
2 answers

When should I prefer `shared_ptr` to `make_shared`?

As pointed in answers to Difference in make_shared and normal shared_ptr in C++, make_shared outperforms shared_ptr in most cases. Then why does C++ standard define both shared_ptr and make_shared together? Are there any cases at which I should…
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
3
votes
3 answers

std::make_shared(), std::weak_ptr and cyclic references

My question is about this claim: If any std::weak_ptr references the control block created by std::make_shared after the lifetime of all shared owners ended, the memory occupied by T persists until all weak owners get destroyed as well, which may…
Vizor
  • 396
  • 3
  • 14
3
votes
2 answers

std::weak_ptr assignment with std::make_shared

I stumbled upon this behaviour when using std::weak_ptr and std::make_shared and I found it a little weird. I am using C++11. #include #include int main() { std::weak_ptr weak; std::shared_ptr shared…
antogilbert
  • 176
  • 2
  • 12
3
votes
2 answers

any difference in constructing shared pointer

what is difference between auto sp = std::make_shared(); auto sp(std::make_shared()); Detailed explanation is needed.
pepero
  • 7,095
  • 7
  • 41
  • 72
3
votes
2 answers

shared_ptr from unique_ptr of abstract class

I'm trying to follow Herb Sutter's C++ guidelines, in this case to prefer unique_ptr to raw pointers and shared_ptr. One of the arguments in favour of std::unique_ptr is convertibility to shared_ptr should that be needed at some point. In my case I…
Tim
  • 4,560
  • 2
  • 40
  • 64
3
votes
0 answers

Why doesn't make_shared work with Variable Length Arrays?

consider the following: I have a class A with a constructor which takes an array of ints of size 3 as argument. Now I want to construct a shared_ptr to A. If I use shared_ptr<>(new A (parameter)) everything is fine. But if I try using…
3
votes
3 answers

c++ std::make_shared with new macro

I use a macro in place of new to get some extra information in debug mode: #if defined(_DEBUG) #define SAGE_NEW new(__FUNCTION__, __FILE__, __LINE__) #else #define SAGE_NEW new #endif I have found this quite useful in custom memory profiling and…
DSM
  • 99
  • 6
3
votes
1 answer

What stops compilers from automatically deducing to use make_shared?

I understand: shared_ptr x = make_shared(); is more efficient than: shared_ptr x(new X()); and I understand the advantages. However, I do not understand why the compiler could not have a rule like "if I see new() in the same line as a…
2
votes
2 answers

When a thread is executing make_shared, can another thread do something to cause a leak of the object that make_shared creates via new?

From Effective Modern C++, Item 21, I learned that one advantage of std::make_shared over new+std::shared_ptr is that code like this processWidget(std::shared_ptr(new Widget), computePriority()); can result in a leaked Widget if…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
2 answers

Why make_shared call the destructor even if there are still weak pointer?

From theory, one difference between make_shared() and shared_ptr is the memory allocation technique. make_shared() should use a single block, while shared_ptr should use two blocks. One make_shared() drawback should be that memory can't be freed if…
Fzza
  • 105
  • 1
  • 9
2
votes
1 answer

Enforcing std::shared_ptr usage via protected constructors and a static member function to create instances and std::make_shared

I have some classes that are only ever used via std::shared_ptr. Instances of these classes are not designed to be used directly by allocating them on the stack or by raw pointers via new. I enforce this currently by making the constructors…