我和我的一位同事找到了两个解决方案,解决了我的问题。
解决方案 1 - hacky 一个
这个想法是使用std::vector<double>的底层实现的结构,它由在我的例子中的3个成员组成,其中包含3个指向向量数据的指针。
- 数据段的起始地址
- 数据段的结束地址
- 数据段当前最大容量的地址
所以我构建了一个包含这三个地址的struct,并将reinterpret_cast 用于std::vector。这适用于我机器上std::vector 的当前实现。此实现可能会有所不同,具体取决于已安装的 STL 版本。
这里的好处是,我可以使用std::vector 的界面而无需创建它。我也不必将数据复制到std::vector。我也可以从存储在我的复杂类中的初始数据中获取一部分。我可以通过发送给结构的指针来控制被操作的部分。
这解决了 我的问题,但它是一个 hack。我可以使用它,因为代码只与我自己相关。我仍然发布它,因为它可能会引起其他人的兴趣。
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
//--------------------------------------------------------------------------//
//--- This function is given, I cannot change its signature.
bool
func_in_lib(std::vector<double>& data, double& res) {
//--- check some properties of the vector
if (data.size() < 10)
return false;
//--- do something magical with the data
for (auto& d : data)
d *= 2.0;
res = 42.0;
return true;
}
//--------------------------------------------------------------------------//
struct DataType {
double a = 1.0;
double b = 2.0;
double c = 3.0;
};
//--------------------------------------------------------------------------//
ostream&
operator<<(ostream& out, const DataType& d) {
out << d.a << " " << d.b << " " << d.c << endl;
return out;
}
//--------------------------------------------------------------------------//
int
main(int argc, char const* argv[]) {
int count = 20;
//--- init and print my data
unique_ptr<DataType[]> my_data = make_unique<DataType[]>(count);
for (int i = 0; i < count; ++i)
cout << my_data.get()[i];
//--------------------------------------------------------------------------//
// HERE STARTS THE UGLY HACK, THAT CAN BE ERROR-PRONE BECAUSE IT DEPENDS ON
// THE UNDERLYING IMPLEMENTATION OF std::vector<T>
//--------------------------------------------------------------------------//
struct VecAccess {
double* start = nullptr; // address to the start of the data
double* stop0 = nullptr; // address to the end of the data
double* stop1 = nullptr; // address to the capacity of the vector
};
//---
DataType* p_data = my_data.get();
VecAccess va{ &(p_data[0].a), //points at the 'front' of the vector
&(p_data[count - 1].c) + 1, //points at the 'end' of the vector
&(p_data[count - 1].c) + 1 };
vector<double>* p_vec_access = reinterpret_cast<vector<double>*>(&va);
//--------------------------------------------------------------------------//
// HERE ENDS THE UGLY HACK.
//--------------------------------------------------------------------------//
//---
double dummy = 0.0; // this is only relevant for the code used as minimum example
func_in_lib(*p_vec_access, dummy);
//--- print the modified data
for (int i = 0; i < count; ++i)
cout << my_data.get()[i];
return 0;
}
更新:分析第二个解决方案的汇编代码表明,即使未调用数据对象的复制构造函数,也会执行内容的副本。复制过程发生在机器代码级别。
解决方案 2 - 移动语义
对于这个解决方案,我必须用noexcept 标记DataType 的移动构造函数。
关键思想是不将DataType[] 数组视为std::vector<double>。
相反,我们将std::vector<double> 视为std::vector<DataType>。我们可以
然后将数据 移动 到这个向量中(不复制),将其发送给函数,
然后再把它移回来。
数据不是复制而是移动std::vector,速度更快。同样与我的案例相关,我可以再次从存储在我的复杂类中的初始数据中获取一部分。
此解决方案的缺点是我必须为
以正确的大小移动数据。
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
using namespace std;
//--------------------------------------------------------------------------//
//--- This function is given, I cannot change its signature.
bool
func_in_lib(std::vector<double>& data, double& res) {
//--- check some properties of the vector
if (data.size() < 10)
return false;
//--- do something magical with the data
for (auto& d : data)
d *= 2.0;
res = 42.0;
return true;
}
//--------------------------------------------------------------------------//
class DataType {
public:
double a = 1.0;
double b = 2.0;
double c = 3.0;
// clang-format off
DataType() = default;
DataType(DataType const&) = default;
DataType(DataType&&) noexcept = default;
DataType& operator=(DataType const&) = default;
DataType& operator=(DataType&&) noexcept = default;
~DataType() = default;
// clang-format on
};
//--------------------------------------------------------------------------//
ostream&
operator<<(ostream& out, const DataType& d) {
out << d.a << " " << d.b << " " << d.c << endl;
return out;
}
//--------------------------------------------------------------------------//
int
main(int argc, char const* argv[]) {
int count = 20;
//--- init and print my data
unique_ptr<DataType[]> my_data = make_unique<DataType[]>(count);
for (int i = 0; i < count; ++i)
cout << my_data.get()[i];
//---
vector<double> double_vec;
double_vec.reserve(count * 3);
//--- here starts the magic stuff
auto& vec_as_datatype = *reinterpret_cast<vector<DataType>*>(&double_vec);
auto* start_mv = &(my_data.get()[0]);
auto* stop_mv = &(my_data.get()[count]) + 1;
//--- move the content to the vec
move(start_mv, stop_mv, back_inserter(vec_as_datatype));
//--- call the external func in the lib
double dummy = 0.0; // is only needed for the code of the example
func_in_lib(double_vec, dummy);
//--- move the content to back
move(begin(vec_as_datatype), end(vec_as_datatype), start_mv);
//--- print modified the data
for (int i = 0; i < count; ++i)
cout << my_data.get()[i];
}