我有一个函数,该函数返回包含std::vector
作为成员的类的std::unique_ptr
。我需要将此向量对象存储在堆上,以便可以通过C风格的DLL接口传递它。
请参见下面的代码示例:
#include <iostream>
#include <cstdlib>
#include <memory>
#include <vector>
// I have control over the following two classes
class SomeBigClassWithManyMembers { };
class MyClass
{
std::unique_ptr<SomeBigClassWithManyMembers> up;
public:
static const std::vector<MyClass> GetMany()
{
// imagine this does lots of work
return std::vector<MyClass>(50);
}
// following code is suggested in https://stackoverflow.com/questions/31430533/move-assignable-class-containing-vectorunique-ptrt but doesn't help
/*
MyClass() { }
MyClass(MyClass&& other): up{std::move(other.up)} { }
MyClass& operator=(MyClass&& other)
{
up = std::move(other.up);
return *this;
}
*/
};
// Imagine that I can't change this C-style code - it's a fixed interface provided by someone else
struct NastyCStyleStruct
{
void* v;
};
void NastyCStyleInterface(NastyCStyleStruct s) { printf("%u", (unsigned int)((std::vector<MyClass>*)s.v)->size()); }
int main()
{
NastyCStyleStruct s;
s.v = new std::vector<MyClass>(std::move(MyClass::GetMany()));
NastyCStyleInterface(s);
return 0;
}
请注意,在我的实际代码中,向量需要使创建它的函数失效(因为这是在DLL中完成的,因此编写)>
就足够了。该矢量必须存储在堆中。auto vec = MyClass::GetMany(); s.v = &vec;
将不是
这里的麻烦是代码似乎试图使用MyClass
的(不存在)复制构造函数。我不明白为什么要调用复制构造函数,因为我明确地要求使用std::move
进行移动语义。甚至没有将s.v
初始化为适当大小的新new std::vector
并调用std::move
的三参数版本也有效。
来自g ++的错误:
In file included from /usr/include/c++/7/memory:64:0, from stackq.cpp:4: /usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = MyClass; _Args = {const MyClass&}]’: /usr/include/c++/7/bits/stl_uninitialized.h:83:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const MyClass*, std::vector<MyClass> >; _ForwardIterator = MyClass*; bool _TrivialValueTypes = false]’ /usr/include/c++/7/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const MyClass*, std::vector<MyClass> >; _ForwardIterator = MyClass*]’ /usr/include/c++/7/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const MyClass*, std::vector<MyClass> >; _ForwardIterator = MyClass*; _Tp = MyClass]’ /usr/include/c++/7/bits/stl_vector.h:331:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = MyClass; _Alloc = std::allocator<MyClass>]’ stackq.cpp:43:65: required from here /usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘MyClass::MyClass(const MyClass&)’ { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ stackq.cpp:10:7: note: ‘MyClass::MyClass(const MyClass&)’ is implicitly deleted because the default definition would be ill-formed: class MyClass ^~~~~~~ stackq.cpp:10:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = SomeBigClassWithManyMembers; _Dp = std::default_delete<SomeBigClassWithManyMembers>]’ In file included from /usr/include/c++/7/memory:80:0, from stackq.cpp:4: /usr/include/c++/7/bits/unique_ptr.h:388:7: note: declared here unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~
如何修复此代码,以便将矢量对象本身存储在堆中?
我有一个函数返回一个包含std :: unique_ptr作为成员的类的std :: vector。我需要将此向量对象存储在堆上,以便可以通过C风格的DLL接口传递它。 ...
这里的问题是GetMany
被定义为返回const std::vector
。即使它是按值返回的,所以如果您对结果进行变异,也不会有不良副作用的风险,编译器仍然会强制执行该类型(auto
只是复制了该函数的确切返回类型),因此无法移动从中。因此,它不必廉价地复制几个指针/ size_t
大小的值(复制到vector
的内容,大小和容量),而是必须进行完整的复制构造,包括复制构造存储在原始vector
中的所有值。 。由于存储实例中的unique_ptr
成员,该操作失败。