拒绝std :: vector删除其数据 [英] Deny std::vector from deleting its data

查看:83
本文介绍了拒绝std :: vector删除其数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

T* get_somthing(){
    std::vector<T> vec; //T is trivally-copyable
    //fill vec
    T* temp = new T[vec.size()];
    memcpy(temp, vec.data(), vec.size() * sizeof(T));
    return temp;
}

我想通过返回<$ c $来摆脱复制过程c> std :: vector :: data 直接像这样:

I want to get rid of the copy process by returning the std::vector::data directly like this:

T* get_somthing(){
    std::vector<T> vec; //T is trivally-copyable
    //fill vec
    return temp.data();
}

但是,这是错误的,因为当<$时数据将被删除c $ c> vec 析构函数被调用。

However, that is wrong since the data is going to be deleted when vec destructor is called.

那么,如何防止vec删除其数据?换句话说,我想要某种从 std :: vector 到C ++ Raw Dynamic Array的移动习惯。

So, how can I prevent vec from delete its data? In other word I want some kind of move-idiiom from std::vector to C++ Raw Dynamic Array.

聚苯乙烯更改设计不是一种选择。使用 std :: vector 是强制性的。将指针返回到 array 也是强制性的。 Becauese它是两个模块之间的包装。一个需求向量,另一个需求指针。

P.S. Changing the design is not an option. Using the std::vector there is mandatory. Returning a pointer to array is also mandatory. Becauese It is a wrapper between two modules. One need vector the other need pointer.

推荐答案


PS。更改设计不是一种选择。使用std :: vector是强制性的。返回数组的指针也是必须的。

P.S. Changing the design is not an option. Using the std::vector there is mandatory. Returning a pointer to array is also mandatory.

更改设计是您的最佳选择。我建议重新考虑这种立场。

Changing the design is your best option. I recommend reconsidering this stance.

(目前)没有办法窃取向量的缓冲区,因此给定(问题中提到的限制(silly ††),复制是可行的方法。

There is (currently) no way to "steal" the buffer of a vector, so given the (silly††) limitations stated in the question, copying is the way to go.

†Tomasz Lewowski链接了一个提案,如果它包含在将来的标准中: http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2015/n4359.pdf (编辑:如前所述,它被c ++ 17拒绝了)

† Tomasz Lewowski linked a proposal that would change this if it is included in a future standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4359.pdf ( as pointed out, it was rejected from c++17)

††直到被具体要求证明是愚蠢的。

†† Silly until justified by concrete requirements.


这是一个包装在两个模块之间。一个需求向量,另一个需求指针。

It is a wrapper between two modules. One need vector the other need pointer.

大概,另一个需要指针的接口将缓冲区的破坏委托给调用者,可能使用了诸如 void delete_somthing(T *)之类的回叫。在我看来,不归还所有权将是非常糟糕的设计。

Presumably, the other interface that needs the pointer, delegates the destruction of the buffer to the caller, possibly using some sort of call back like void delete_somthing(T*). Taking ownership without giving it back would have been very bad design, in my opinion.

如果您确实控制了销毁,则可以将矢量存储在地图中,并在传递指针进行销毁时删除向量:

In case you do have control of the destruction, you can store the vector in a map, and erase the vector, when the pointer is passed for destruction:

std::unordered_map<T*, std::vector<T>> storage;

T* get_somthing(){
    std::vector<T> vec; //T is trivally-copyable
    //fill vec
    T* ptr = vec.data();
    storage[ptr] = std::move(vec);
    return ptr;
}

void delete_somthing(T* ptr){
    storage.erase(ptr);
}

这篇关于拒绝std :: vector删除其数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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