Questions tagged [scoped-ptr]

A smart pointer provided by Boost that owns a dynamically-allocated pointer and cannot be copied or moved, retaining ownership of the pointer in its scope.

The Boost Smart Pointers library provides the class template boost::scoped_ptr, a smart pointer with non-transferable single-ownership semantics.

scoped_ptr is not in the C++ Standard Library, but const std::unique_ptr is a better replacement for scoped_ptr and scoped_array.

42 questions
60
votes
2 answers

Difference between boost::scoped_ptr and std::unique_ptr

Is the sole difference between boost::scoped_ptr and std::unique_ptr the fact that std::unique_ptr has move semantics whereas boost::scoped_ptr is just a get/reset smart pointer?
moshbear
  • 3,162
  • 1
  • 17
  • 32
38
votes
5 answers

shared_ptr vs scoped_ptr

scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr. So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know…
Narek
  • 35,407
  • 69
  • 202
  • 359
23
votes
3 answers

shared_ptr with malloc and free

I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent…
user765443
15
votes
3 answers

Why does boost not have a make_scoped()?

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 example from the boost::scoped_ptr documentation that…
Drew Dormann
  • 50,103
  • 11
  • 109
  • 162
10
votes
2 answers

C++: Can boost::scoped_ptr be initialized inside a constructor?

Can a class member of type boost::scoped_ptr be initialized inside the class' constructor? How? (Not in the initialization list)
Jonathan
  • 84,911
  • 94
  • 244
  • 345
10
votes
4 answers

Why scoped pointers in boost

What is the objective of scoped pointer? to my understanding, the scoped pointer manages the memory within a block of code. If i want to declare a variable within a block , i can just declare it on a stack and not worry about cleaning.
Jimm
  • 7,173
  • 14
  • 59
  • 106
8
votes
5 answers

Destructor not invoked when an exception is thrown in the constructor

Why is the destructor not invoked in this code? #include #include class MyClass { boost::scoped_ptr ptr; public: MyClass() : ptr(new int) { *ptr = 0; throw; std::cout<<"MyClass Allocated\n"; } ~MyClass() {…
cppcoder
  • 19,480
  • 6
  • 42
  • 77
8
votes
1 answer

When is a type considered complete?

Consider the following code snippet. The destructor of boost::scoped_ptr is invoked at the end of the main function. The destructor uses boost::checked_delete to deallocate the encapsulated Widget pointer. #include #include…
CppNoob
  • 1,996
  • 20
  • 33
7
votes
3 answers

Is it good practice to pass scoped_ptr by reference (from one method to another inside a class)?

Or if i need to do that, then i should just use shared_ptr?
amitlicht
  • 2,658
  • 4
  • 20
  • 25
6
votes
3 answers

pimpl-idiom in template; which smart pointer?

I usually use a boost::scoped_ptr for pimpl's (for one reason because then I don't get surprises if I forget to deal with the copy constructor) With templates however I can't just put the destructor in the cpp file where the impl is fully defined…
odinthenerd
  • 4,964
  • 1
  • 22
  • 53
6
votes
2 answers

destructor not called in case of exception with scoped_ptr

I have just started using c++ boost libraries. I read in many places that when a scoped_ptr is used then the object is always destroyed even in case of exceptions. They behave much like built-in C++ pointers except that they automatically delete…
leonidus
  • 236
  • 1
  • 7
5
votes
3 answers

Which header should be used for using scoped_ptr

I want to use smart pointer in my c++ application. Which header file I should include for using std scoped_ptr?
Vinod
  • 1,026
  • 2
  • 16
  • 32
5
votes
4 answers

Is it an overkill to use scoped_ptr in simple cases?

I am using scoped_ptr inside small functions like this. so that I don't have to call delete. Is this an overkill for this usage? My team members prefer raw pointers and delete. What is the cost of using scoped_ptr if this happens to be used in a…
balki
  • 22,482
  • 26
  • 85
  • 135
4
votes
4 answers

why boost::scoped_ptr is cleared in a singleton implementation

I implemented a simple singleton by using boost::scoped_ptr: template class Singleton : public boost::noncopyable { public: static T& instance() { boost::call_once(init, flag); return *t; } …
eric zhu
  • 51
  • 6
4
votes
2 answers

Why I cant return Boost::Scoped_ptr from function?

So I try to create some wrapper around boost.extension functions for class creation. So I have created a function: template boost::scoped_ptr get_class (shared_library & lib, std::string…
Rella
  • 59,216
  • 102
  • 341
  • 614
1
2 3