Questions tagged [make-shared]

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

138 questions
2
votes
3 answers

Data cache implications of using std::make_shared()

I read here that: 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 naked…
tommyk
  • 3,187
  • 7
  • 39
  • 61
1
vote
2 answers

make_shared create std::shared_ptr? gcc 4.6.2

i'm using gcc 4.6.2. I'm trying to push_back in a vector shared_ptr's. But gcc gives me everytime an error. Here my codelines: std::vector< std::tr1::shared_ptr > procs; std::string line; while (getline(file, line) && line.find(JobMask) !=…
demonking
  • 2,584
  • 5
  • 23
  • 28
1
vote
2 answers

make_shared returns error C2665: no overloaded function could convert all the argument types

A sample question on c++ primer: Add member named get_file that returns a shared_ptr to the file in the QueryResult object. class QueryResult { friend std::ostream& print(std::ostream&, const QueryResult&); public: using line_no =…
gaofeng
  • 393
  • 1
  • 3
  • 11
1
vote
3 answers

Conditional class used in a make_shared with same parameters

Consider this piece of code creating, based on a condition, a different class instance through a std::make_shared. Note that the two possible classes used (Child1 and Child2) are having the same construction parameters. class Base { public: …
jpo38
  • 20,821
  • 10
  • 70
  • 151
1
vote
1 answer

Undefined reference to initialized static member variable with make_shared

Compiling with -std=c++14 the following code: #include class A { public: static constexpr int c = 0; std::shared_ptr b; A() { b = std::make_shared (c); } }; int main () { A a; return…
1
vote
0 answers

Why does std::make_shared cause linker errors when using non-inline static const members?

I'm using C++17 and stumbled on a linker error with this kind of code #include struct SomeType { static const int MIN = 0; static const int MAX = 0; }; struct Range { Range(int min=0, int max=0) : min(min), max(max) {} …
Allan Ojala
  • 694
  • 10
  • 22
1
vote
2 answers

Error when casting derived class to protected base interface in std::make_shared

I have this code: #include class SomeInterface { public: virtual void VirtualMethod() {} virtual void PureVirtualMethod() = 0; }; class SomeInterfaceDependent { public: explicit SomeInterfaceDependent(SomeInterface* a) :…
mooko
  • 109
  • 1
  • 4
1
vote
2 answers

What does an default initialization of an array of shared_ptrs do?

When I create an array of shared_ptrs to object Foo, what is actually being created? class Foo { public: Foo() x(3) : {}; int x; } std::array, 10> arr_; For instance, will the following code print 3? std::shared_ptr
John
  • 23
  • 4
1
vote
1 answer

How to use shared_ptr and make_shared with arrays?

I want to use a C++ shared_ptr as a replacement for raw C pointers. As a simple example the following code seems to work as intended: from libcpp.memory cimport shared_ptr, allocator cdef shared_ptr[double] spd cdef allocator[double]…
oli
  • 659
  • 1
  • 6
  • 18
1
vote
1 answer

Returning make_shared, inheritence and casting to Derived not working as expected?

Consider this example constructing shared_ptr in various ways and returning: #include #include class Base { public: virtual ~Base() {} Base(int y) : y_(y) {} int y_; }; class Derived : public Base { public: …
lfgtm
  • 1,037
  • 6
  • 19
1
vote
1 answer

Using copy-constructor in conjuction with std::make_shared

I have defined a simple class Integer containing an int value, calling std::make_shared(&ref) will force the program to use the constructor accepting an int. Did I implemented a wrong copy-constructor or there's something wrong with my…
1
vote
0 answers

Possible memory leak with shared_ptr C++

I am trying to debug possible memory leak caused by following line in my code: DeserializeRegex["Grp1"][strPtr] = std::shared_ptr(operator new(10), [](void *pi) { delete pi; }); I think one possible cause can be using shared_ptr instead of…
user1228352
  • 569
  • 6
  • 21
1
vote
1 answer

boost::make_shared fail but std::make_shared work

I was doing an echo-server example in boost::asio. But using boost::make_shard will cause "unknown exception" while std::make_shared will not. See the commented line. I am using visual studio 2017 and boost 1.67.The commented part will crash while…
YNX
  • 511
  • 6
  • 17
1
vote
1 answer

Why std::make_shared different with new when construct with static constexpr member?

#include #include #include class Node{ public: static constexpr int data_size = sizeof(int); }; class View{ public: View(int size){ } }; class Header: public Node{ public: void foo(){ …
everettjf
  • 116
  • 2
  • 8
1
vote
2 answers

C++0x and Friend functions and boost::make_shared

If I have a class with a private construction, using boost::make_shared() to construct a shared_ptr of that class from within a member function of that class will issue a compiler error using gcc 4.6. #include "boost/shared_ptr.hpp" #include…
Raindog
  • 1,468
  • 3
  • 14
  • 28