Now std::vector, does not have any guarantees about using move semantic during objects reallocation (like on vector grow).
I agree but I think this is an issue for standard library implimentor and not compiler writing. The only issue with this change is support for pre-C++ 11 code before move semantics was define. The other issue is which move should be used?
If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T or by any InputIterator operation there are no effects.If an exception is thrown while inserting a single element at the end and T is CopyInsertableor is_nothrow_move_constructible_v<T> is true, there are no effects.Sure looks like a guarantee to me.Where exactly is this kind of wording missing?
See, I don't think you should do that.
A move should be indistinguishable from a copy - if you don't look at
the old object, it is just a faster copy, after all.
We should move, but which (current) implementation doesn't do move?
See, I don't think you should do that.
A move should be indistinguishable from a copy - if you don't look at
the old object, it is just a faster copy, after all.So, move = faster copy
template<typename T, T v = T{}>
struct MoveIt
{
MoveIt() : t{v} {}
MoveIt(T t_) : t{std::move(t_)} {}
operator T&() { return t; }
operator T const&() const { return t; }
MoveIt(MoveIt const&) = default;
MoveIt(MoveIt&& that) : MoveIt{static_cast<MoveIt const&>(that)} { that.t = v; }
MoveIt& operator=(MoveIt const&) = default;
MoveIt& operator=(MoveIt&& that) { *this = static_cast<MoveIt const&>(that); that.t = v; return *this; }
private:
T t;
};
If an object is the same object if and only if it has the same pointer address, then
std::vector always copies when it reallocates.
I think that's a reasonable ask. You just have to write good examples of which
conditions would benefit from this. One of them is that copy constructors
usually have more work to do.
Okay, I think I start to understand your motivation. You think, that having both constructors available to use during reallocation phase will theoretically allow compiler to choose faster path? Sounds very vague... What motivation, then, to write move constructor in class, if it is slower beforehand?
Other examples with cases were both the copy constructor and move constructor are noexcept, are:
std::vector<std::vector<T>>
std::vector<std::string>
std::vector<std::function>
On segunda-feira, 30 de outubro de 2017 16:05:34 PDT Nevin Liber wrote:
> I don't think that holds in general if both the copy constructor and move
> constructor are noexcept. The shared_ptr case is an anomaly (but an
> important one) in that the copy constructor has less but slower work to do
> (copy pointers and increase an *atomic* refCount), while the move
> constructor has more but faster work to do (copy pointers in this and clear
> pointers in the source), because it has to modify two objects.
My gut feeling is that noexcept move constructors do just a little less work
then their equivalent noexcept copy constructor.
So enforcing a call to the
move constructor is probably a gain, but not by much.
Also, please do point me where standard says something about vector reallocation behavior at all.
Thank you. Is it in public domain? Can't google it, and honestly, first time hear of it...
Also, where it said so?
вторник, 31 октября 2017 г., 17:26:42 UTC+2 пользователь Hyman Rosen написал:
The standard library should be specified by reference implementation.
So... If I convinced some of you that standard need stronger guarantees about move in vector... Or at least some defined behavior...
[I personally think that allow std::vector<std::vector<T>> to use copy on reallocation is not sane]
Well, I don't know the procedure... But as I can guess, only committee member may submit proposal / defect report.