[英]Initialize std::vector with given array without std::allocator
I am using an external library which provides a function with the following interface:我正在使用一个外部库,它提供了一个具有以下接口的 function:
void foo(const std::vector<int>& data);
I am receiving a very large C-style array from another library which has already been allocated:我从另一个已经分配的库中收到了一个非常大的 C 样式数组:
int* data = bar();
Is there any way for me to pass on data
to foo
without allocating and copying each element?有什么方法可以让我在不分配和复制每个元素的情况下将data
传递给foo
? data
is very large and therefore I want to avoid a copy and allocation if possible. data
非常大,因此我想尽可能避免复制和分配。
I could have used allocators, but foo
is not templated for an allocator, so I don't believe this is possible.我本可以使用分配器,但foo
没有为分配器模板化,所以我不相信这是可能的。
I understand I may be asking for magic, but if it is possible that would be great.我知道我可能会要求魔法,但如果可能的话,那就太好了。 Of course if foo
rather took an std::span
this would not be a problem.当然,如果foo
宁愿使用std::span
这将不是问题。
Thanks!谢谢!
Edit: Make ```foo`` take a const reference instead of just a reference.编辑:使 ```foo`` 采用 const 引用,而不仅仅是一个引用。
Is there any way for me to pass on data to foo without allocating and copying each element?有什么方法可以让我在不分配和复制每个元素的情况下将数据传递给 foo?
No, there's no way of avoiding it given the premise.不,鉴于前提,没有办法避免它。
There are two alternative workarounds by changing the premise:通过更改前提,有两种替代解决方法:
foo
to not require a vector.将foo
更改为不需要向量。 As you point out, std::span
would probably be a reasonable choice.正如您所指出的, std::span
可能是一个合理的选择。This answer is a magic depented on implementation of compiler.这个答案是依赖于编译器实现的魔法。
We can forcely access the contain of vector.我们可以强制访问向量的包含。
Take g++ as an example, it use 3 protected pointers, _M_start
, _M_finish
, and _M_end_of_storage
to handle storage.以 g++ 为例,它使用 3 个受保护的指针_M_start
、 _M_finish
和_M_end_of_storage
来处理存储。 So we can create a derived class that set/reset the pointers to retuen of bar()
in ctor/dtor.所以我们可以创建一个派生的 class 来设置/重置指向 ctor/dtor 中bar()
的 retuen 的指针。
Example code for g++: g++ 的示例代码:
static_assert(__GNUC__ == 7 && __GNUC_MINOR__ == 5 && __GNUC_PATCHLEVEL__ == 0);
class Dmy: public std::vector<int>{
public:
Dmy(int *b, int *e)
{
_M_impl._M_start = b;
_M_impl._M_finish = e;
_M_impl._M_end_of_storage = _M_impl._M_finish;
}
~Dmy()
{
_M_impl._M_start = 0;
_M_impl._M_finish = 0;
_M_impl._M_end_of_storage = 0;
}
};
foo(Dmy(data,end_of_data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.