std::在 std::shared_ptr 之间交换,<A>其中 A 具有动态数组
std::swap between std::shared_ptr<A> where A has dynamic array
首先,我的代码:
struct A {
A(int size);
~A();
A(A&& other);
A& operator=(const A& other);
uint8_t* data = nullptr;
};
A::A(int size)
{
data = new uint8_t[size];
}
A::~A()
{
delete [] data;
data = nullptr;
}
A::A(TPixel &&other)
{
data = other.data;
}
A& A::operator=(const A& other)
{
data = other.data;
}
我有两个变量
std::shared_ptr<A> a = std::make_shared<A>(5);
std::shared_ptr<A> b = std::make_shared<A>(5);
我尝试了std::swap(a, b);
并发现Valgrind的错误: std::enable_if<std::__and_<std::is_move_constructible<A*>, std::is_move_assignable<A*> >::value, void>::type std::swap<A*>(A*&, A*&)
为什么我会遇到这个错误?我已经实现了移动操作员,当我测试std :: is_move_assignable和std :: is_move_constructible时,返回值是真实的。
发现Valgrind中的错误:
std::enable_if<std::__and_<std::is_move_constructible<A*>, std::is_move_assignable<A*> >::value, void>::type std::swap<A*>(A*&, A*&)
为什么我会遇到此错误?
您显示的不是错误。这是函数声明。
我已经实施了移动操作员
您尚未实施移动分配运算符。
P.S。
- 您尚未定义移动构造函数。
- 您已经定义了未声明的构造函数:
A::A(TPixel &&)
。这可能是相关的。
- 您已经定义了未声明的构造函数:
- 复制分配运算符
- 泄漏内存。
- 将两个对象都指向同一数组。
- 如果对象被拷贝分配并且副本已经被销毁,则驱动器具有不确定的行为。
相关文章:
- 创建一个 const std::vector ,其中包含另一个 const std::vector 和其他值
- std::任何只用于移动的模板,其中副本ctor内的static_assert等于编译错误,但为什么
- 为什么 std::get<T> 其中 T 是调用 constexpr 函数失败的结果?
- 构造函数模板参数推导,其中 std::function 作为参数
- 如何设置一个 std::vector 与另一个,其中两个是不同类的向量?
- std::p romise<T> 其中 T 必须在 Visual Studio 2017 中默认构造?
- 我如何将std ::数组作为模板参数传递,其中C 中的元素数量不同
- 如何在我的源代码中找到所有发生的情况,其中std :: string被传递给方法作为副本而不是恒定引用
- 访问 std::string 中的元素,其中字符串的位置大于其大小
- std::在 std::shared_ptr 之间交换,<A>其中 A 具有动态数组
- 如何创建一个 std::tuple,其中包含由索引元组指定的向量中的成员
- 如何创建 std::vector of char/std::byte,其中第一个字节对齐到 16 个字节,但没有填充
- 拥有 std::map 的最佳方式,我可以在其中定义如果没有键时返回的内容
- 创建一个随机的std ::向量,其中一个和零C
- 如何在其中一个层次结构类中使用std :: is_base_of
- 传递std ::向量的派生类型,其中预期基本类型的std ::向量
- 初始化静态std :: multimap在其中声明的方法
- 多维 std::intializer_list,其中维度数被指定为其类的模板参数
- 如何制作一个简单的C++程序,其中std::cout不被刷新
- 当 std::vector 增长时,其中元素的地址不再有效