当std :: vector重新分配其内存数组时,使用复制构造函数还是move构造函数? [英] When std::vector reallocate its memory array, is copy constructor or move constructor used?

查看:64
本文介绍了当std :: vector重新分配其内存数组时,使用复制构造函数还是move构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: vector重新分配其内存数组时,使用哪种复制/移动构造函数将元素复制/移动到新房子?

When std::vector reallocate its memory array, what kind of copy / move constructor is used to copy / move elements to new houses?

推荐答案

如果move-constructor存在并且为 noexcept ,则使用它.否则,将使用复制构造函数.

If the move-constructor exists and is noexcept then it is used. Otherwise the copy-constructor is used.

使用可能会抛出的move-constructor是不可取的,因为可能会将某些对象移动到新存储中,然后发生异常,从而阻止其余对象被移动.

Using a move-constructor that might throw is undesirable as it might happen that some objects are moved to the new storage and then an exception prevents the rest of the objects being moved.

cppreference.com网站确实说过,如果该对象是不可复制的,但具有非noexcept move构造函数,则它将使用该move-constructor,如果引发异常,则带有未指定的行为".我想这意味着元素可能会从向量中丢失.

The cppreference.com site does say that if the object is non-copyable , but has a non-noexcept move constructor, then it will use that move-constructor, with "unspecified behaviour" if an exception is thrown. I guess that means elements may be lost from the vector.

这篇关于当std :: vector重新分配其内存数组时,使用复制构造函数还是move构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆