gpt4 book ai didi

c++ - 在不复制的情况下将 unique_ptr 的数据释放并转换为另一个

转载 作者:行者123 更新时间:2023-11-30 04:48:23 24 4
gpt4 key购买 nike

我有一个 float 组的 unique_ptr,我想在我的函数中将其更改为 uint8_t 数组的 unique_ptr。换句话说,unique_ptr<float[]> --> unique_ptr<uint8_t[]> .

这是我试过的

void my_function(std::unique_ptr<float*> data) {
std::unique_ptr<uint8_t*> converted_data(reinterpret_cast<uint8_t*>(data.release()));

// ...
// Processing converted_data ...
}

因为我收到这个错误: no matching constructor for initialization of 'std::unique_ptr<uint8_t *>' ,我想知道是否可以在不复制数据的情况下实现这个目标。

最佳答案

您正在转换为不兼容的类型。

data.release() 是 float** 类型,因为它返回一个指向 unique_ptr 中任何内容的原始指针(在本例中为 float*)。您应该改为转换为 uint_8t**(尽管这看起来仍然是个坏主意)。

关于c++ - 在不复制的情况下将 unique_ptr 的数据释放并转换为另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55818729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com