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
4
votes
3 answers

scoped pointers and reset

I am playing around with boost scoped pointers and I don't understand this behaviour: #include #include int main() { boost::scoped_ptr p{new int{1}}; std::cout << &p << '\n'; p.reset(new int…
Jacopo
  • 43
  • 3
4
votes
1 answer

Any advantage to using shared_ptr over scoped_ptr at top level?

There is some disagreement in my team about pointer container usage for a particular context. Please consider: int main() { // Top level. This is an important fact to the context // i.e. that the following instance is at this level // so…
Benedict
  • 2,771
  • 20
  • 21
3
votes
1 answer

C++ polymorphism with boost scoped_ptr

Why does the following code not allow foo(ptr) to be called ? #include struct A { virtual ~A() {} }; struct B: public A {}; void foo(boost::scoped_ptr& a) {} void goo(A& a) {} int main() { boost::scoped_ptr
ATemp
  • 319
  • 3
  • 10
3
votes
2 answers

How to create map of scoped_ptr in c++

I have the following code and line 2 gives me an error during compilation. Is it possible to create a map of scoped pointers or do I have to use shared pointers instead? map mp; mp[1] = scoped_ptr(new…
mkmostafa
  • 3,071
  • 2
  • 18
  • 47
3
votes
2 answers

scoped_ptr for structure with substituted free method

I have a structure typedef struct myStruct_st { int a; }myStruct; It can be created using myStruct * myStruct_new() { printf("Allocate\n"); return new myStruct; } And deleted using static void myStruct_free(myStruct * ptr) { …
Max
  • 65
  • 5
2
votes
1 answer

How can I get the address of scoped_ptr?

I'm studying smart pointers, in particular scoped_ptr. I read about the operators * and ->. I tried to run this code: int main(){ boost::scoped_ptrnumber(new int); *number = 432; std::cout<<"Value: "<<*number <
linofex
  • 340
  • 1
  • 2
  • 17
2
votes
2 answers

Why don't smart pointers have conversion operator back to the base pointer?

If often find myself using code like this: boost::scoped_ptr f(new TFoo); Bar(f.get()); // call legacy or 3rd party function : void Bar (TFoo *) Now, I think the smart pointers could easily define an implicit conversion operator back to the…
Roddy
  • 66,617
  • 42
  • 165
  • 277
2
votes
1 answer

_BLOCK_TYPE_IS_VALID error on boost::scoped_array

After a huge amount of digging and searching I found the root of my problem. In essence this code is executed and, in its own project it causes the same error. I see that I cannot reset a smart pointer to a new string...but why? Also is there a…
Lindenk
  • 458
  • 2
  • 6
  • 14
1
vote
5 answers

Some questions about shared_ptr, scoped_ptr and shared_array

I have some questions about smart pointers implemented in boost library. Is the only diffrence between shared_ptr and scoped_ptr that scoped_ptr doesn't have copy constructor and shared_ptr has it? Should i use then scoped_ptr instead of shared_ptr…
fex
  • 3,488
  • 5
  • 30
  • 46
1
vote
4 answers

Weak reference to a scoped_ptr?

Generally I follow the Google style guide, which I feel aligns nicely with the way I see things. I also, almost exclusively, use boost::scoped_ptr so that only a single manager has ownership of a particular object. I then pass around naked…
Shane
  • 3,051
  • 3
  • 40
  • 45
1
vote
1 answer

C++: How to unit test a scoped_ptr?

Suppose I have implemented a scoped_ptr: template class scoped_ptr { public: scoped_ptr() = delete; explicit scoped_ptr(T *ptr) : _ptr(ptr){}; ~scoped_ptr() { delete _ptr; _ptr = nullptr; }; scoped_ptr(const…
tjysdsg
  • 656
  • 8
  • 19
1
vote
3 answers

Which non-shared Smart Pointer for class member variables

When I have a class that contains pointers as member variables what type of smart pointer should they have if I want don't want to use plain pointers? They do not need to be shared (so no shared_ptr necessary). scoped_ptr won't work since I often…
tyrondis
  • 3,364
  • 7
  • 32
  • 56
1
vote
0 answers

scoped_ptr and the c++ standard library

is there a reason why scoped_ptr isn't part of the standard library? are there any plans about it in the future? Some cases I prefer to use scoped_ptr over unique_ptr since it is non-movable unlike the later.
Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
1
vote
1 answer

most STLish way to init array with boost intrusive pointers

I have this: struct Node; typedef boost::intrusive_ptr NodeSPtr; ... boost::scoped_array nodes(new NodeSPtr[size]); ... // "pollute" operations ... ... // reset all the items in the array for (size_t i = 0; i < size; ++i) …
gsf
  • 6,612
  • 7
  • 35
  • 64
1
vote
1 answer

Why are boost::scoped_ptr or std::unique_ptr non-copyable?

In boost::scoped_ptr, says "It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics." It is done through some non-copyable mechanism. My question is why is there requirement…
thassan
  • 391
  • 3
  • 15