std::unique_ptr
- is better to use when you want to transfer ownership
- has no overhead (memory or CPU)
- is better when the initial size is unknown as it allows for dynamic growth
- keeps track of the size for you
Posted 28 June 2012 - 06:06 AM
Posted 28 June 2012 - 06:51 AM
Posted 28 June 2012 - 09:36 AM
Posted 28 June 2012 - 10:30 AM
Edited by Zoomulator, 28 June 2012 - 10:31 AM.
Posted 28 June 2012 - 11:08 AM
You are correct.I'm pretty sure the default deleter does a delete[] on array unique_ptr's. The template is specialized for array types.
Posted 28 June 2012 - 11:23 AM
Only difference is that the unique_ptr enforces the ownership a bit more by not being implicitly copyable.
Edited by larspensjo, 28 June 2012 - 11:30 AM.
Posted 28 June 2012 - 11:43 AM
Please explain in what way it is misleading? std::vector is used to manage vectors. std::unique_ptr is used to manage pointers to objects or vectors. So it is rather obvious what I was asking for, isn't it?First of all, the title of this message is very misleading.
I don't understand this. A vector is a one dimensional array. So for the one dimensional case, there are no differences by definition.Fortunately there is a good general guideline:
Use std::vector!
If you need to know why, please search for some of the discussion about arrays vs. vector and remove all arguments against arrays that concern lifetime management of the array's memory.
Posted 28 June 2012 - 11:58 AM
Thanks for some feed-back, but there is still no explanation on why I should use std::vector instead of std::unique_ptr for managing vectors.
Posted 28 June 2012 - 12:24 PM
Posted 28 June 2012 - 03:39 PM
Posted 28 June 2012 - 04:19 PM
Edited by jonathanjansson, 28 June 2012 - 04:33 PM.
Posted 28 June 2012 - 07:55 PM
Yes, that is legal.Is it allowed to use &v[0], where v is a std::vector?
That would certainly be an important consideration, and possibly a determining factor.It seems to me then that the advantage of std::unique_ptr (compared to std::vector) is when you want to ensure it is not copied?
Posted 29 June 2012 - 02:21 AM
First, initializing a variable with the result of a function calls the move constructor, which std::vector has.I tested with a function that defined a local std::vector, initialized it, and then returned it. Initializing a variable with the result of this function did indeed reference the same memory area, which means there was an implicit move. I wonder how that works (though it did what I wanted).
Posted 29 June 2012 - 05:53 AM
It is not only allowed, it is considered idiomatic.One use-case I have, is to create a vector of data that need to be temporarily forwarded externally (in this case a vertex attribute list forwarded to an OpenGL VBO). Using std::unique_ptr, I can use the get() function to get a pointer. Is it allowed to use &v[0], where v is a std::vector?
A big advantage of using a vector is that you're dealing with a first-class object. It tells the reader "I have a buffer and I'm using it to hold data". Using any kind of pointer instead and you tell the read "I have an address to some memory somewhere, and I'm using that address to do stuff indirectly to that memory". Turns out in the world of professional software development, much more time is spent in maintenance than in development. Being able to communicate intent as clearly as possible is the mark of the best code. the more you can work in the problem domain rather than the implementation domain, the clearer your intent tends to be.It seems to me then that the advantage of std::unique_ptr (compared to std::vector) is when you want to ensure it is not copied?
Posted 29 June 2012 - 06:50 AM
I certainly appreciate that, having used Google Go rather than C++ a couple of years now. So far, my productivity has been about 3 times as high with Go as with C++. Proper use of smart pointers and containers in C++ is an important step towards that goal.Turns out in the world of professional software development, much more time is spent in maintenance than in development. Being able to communicate intent as clearly as possible is the mark of the best code. the more you can work in the problem domain rather than the implementation domain, the clearer your intent tends to be.
Posted 29 June 2012 - 09:50 AM
Please explain in what way it is misleading? std::vector is used to manage vectors. std::unique_ptr is used to manage pointers to objects or vectors. So it is rather obvious what I was asking for, isn't it?
I don't understand this. A vector is a one dimensional array. So for the one dimensional case, there are no differences by definition.
Posted 29 June 2012 - 04:12 PM
In C++11 this can now be replaced with the data() method, which returns a pointer to the first value just like &v[0], but with a bit clearer intent. It's also a const pointer, which &v[0] doesn't provide.It is not only allowed, it is considered idiomatic.One use-case I have, is to create a vector of data that need to be temporarily forwarded externally (in this case a vertex attribute list forwarded to an OpenGL VBO). Using std::unique_ptr, I can use the get() function to get a pointer. Is it allowed to use &v[0], where v is a std::vector?
Edited by Zoomulator, 29 June 2012 - 04:14 PM.
Posted 01 July 2012 - 08:07 PM
Edited by Matt-D, 01 July 2012 - 08:16 PM.
Posted 02 July 2012 - 12:42 AM
More importantly, it works when the vector is empty too!In C++11 this can now be replaced with the data() method, which returns a pointer to the first value just like &v[0], but with a bit clearer intent. It's also a const pointer, which &v[0] doesn't provide.