make_shared is a function for creating shared pointers introduced in C++11.
Questions tagged [make-shared]
138 questions
2
votes
1 answer
C++: How to partially deduce template arguments from make_shared
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…

mutableVoid
- 1,284
- 2
- 11
- 29
2
votes
1 answer
How to use make_shared to create an array of objects of the same type?
We could use "make_shared" to create an object faster and safer compared to use "new". For example,
shared_ptr p = make_shared("Luther").
If I need to create an array of objects (e.g. Dog[3]), is it possible to use "make_shared" instead…

Linda
- 37
- 5
2
votes
2 answers
In what sense does a weak_ptr 'own' a shared_ptr?
I was trying to write a caption for this question for like 10 minutes and as you will see I eventually failed. [Editor's note: I think I have fixed that.]
I was reading Herb Sutter's blog and the topic is using std::make_shared and its cons and…

Eduard Rostomyan
- 7,050
- 2
- 37
- 76
2
votes
1 answer
How do I pass protobuf's boost::shared_ptr pointer to function?
I have to pass a boost::shared_ptr:
boost::shared_ptr pProfile =
boost::make_shared();
which is protobuf's pointer, to a protobuf's function oPerson.set_allocated_profile(pProfile) but…

CMouse
- 130
- 3
- 19
2
votes
2 answers
boost make_shared without template argument
I'm trying to pass a pointer to a stack variable to a function (I don't control) that only takes a boost::shared_ptr.
According to this answer, using boost::make_shared is the way to go. In order to test out this functionality I wrote…

simplename
- 717
- 7
- 15
2
votes
1 answer
Singleton - Impl. using static class member vs. static member variable
Bring you two different implantation of singleton design pattern:
static class member
class SingletonCSM;
using SingletonCSMSp = std::shared_ptr < SingletonCSM > ;
class SingletonCSM
{
public:
~SingletonCSM() { spInstance = nullptr; }
…

idanshmu
- 5,061
- 6
- 46
- 92
2
votes
0 answers
std::make_shared and private constructor
I'm struggling with a (let me say) corner case of the std::make_shared function and I didn't manage to find a viable alternative to my (not-so-serious) problem.
The following code compiles fine:
#include
class A {
A() = default;
…

skypjack
- 49,335
- 19
- 95
- 187
2
votes
1 answer
How is alignment of objects and shared_ptrs calculated?
Say I have an object managed by a shared pointer: shared_ptr. Lets say my X class is 98 bytes large, with the last data member at byte 97-98 (a char).
Generally speaking the shared ptr contains a raw pointer to my X object and a raw pointer to a…

user997112
- 29,025
- 43
- 182
- 361
2
votes
1 answer
Using make_shared with shared_ptrs only beneficial for T < 56 bytes?
As I understand it if you use std::make_shared it creates the reference counting object at the same time as the underlying object.
However, if the object pointer to by the smart_ptr is greater than 56 bytes wouldn't the reference counter end up…

intrigued_66
- 16,082
- 51
- 118
- 189
2
votes
1 answer
Initialize std::array with make_shared
Background
I am writing a driver for a networkprotocol and have a function write(std::shared_ptr package), where package is std::array (0=>header, 1=> body). For convenience I want to write a function write(buffer_ptr body),…

ted
- 4,791
- 5
- 38
- 84
2
votes
1 answer
make_shared with template constructor
I have a class with a template constructor, and want to have a shared_pointer to it. Such as:
class A;
typedef std::shared_ptr A_ptr;
class A {
public:
template
A(Arg1 arg1, Arg2 arg2, T data) { ... do something with data…

Benjamin Cecchetto
- 65
- 1
- 8
2
votes
1 answer
is there a reason why std::make_shared would require a default constructor?
I'm trying to figure out if this is a requirement from cereal or not.
I keep getting errors that class Constructors (default ones) are private, which I've put there for a reason.
However, the originating line for the error, seems to be,…

Ælex
- 14,432
- 20
- 88
- 129
2
votes
1 answer
Variadic versions of std::make_shared calling destructor in iOS
The following...
class TestClass
{
public:
TestClass(const char* szParam, int nParam)
: m_strParam(szParam)
, m_nParam(nParam)
{
Dbg_Printf("2 param constructor - %s, %d\n", m_strParam.c_str(), m_nParam);
}
…

Shammi
- 492
- 5
- 13
2
votes
1 answer
How to assign in c++ an object created in python?
I've basically have a very simple node test case I'm trying to fix.
I have a simple Node class with a getChild and a getParent
The child can be assigned via the addChild function
Then this function automatically set the parent on the relevant class…

user2111835
- 21
- 1
2
votes
0 answers
intrusive_ptr, shared_ptr performance tests
class X {
public:
std::string name;
int age;
long references;
X(string n, int a) : references(0), name(n), age(a) {}
};
inline void intrusive_ptr_add_ref(X* x){
++x->references;
}
inline void intrusive_ptr_release(X* x){
if(--x->references ==…

Michael
- 673
- 2
- 5
- 23