With a colleague of mine I found two solutions, that solve my problem.
Solution 1 - The hacky one
The idea is to use the structure of the underlying implementation of the std::vector<double>
, which consists in my case of 3 members containing 3 pointers to the data of the vector.
- start address of the data section
- end address of the data section
- address of the current maximum capacity of the data section
So I build a struct
containing these three addresses and use a reinterpret_cast
to a std::vector
. This works with the current implementation of std::vector
on my machine. This implementation can vary, depending on the installed version of the STL.
The nice thing here is, that I can use the interface of std::vector
without creating it. I also do not have to copy the data into a std::vector
. I could also take a just part from the initial data stored in my complex class. I can control the manipulated part, by the pointers I send to the struct.
This solves my problem, but it is a hack. I can use it, because the code is only relevant for myself. I still post it, because it could be of interest for others.
#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;
}
Update: Analyzing the assembler code of the second solution shows, that a copy of the content is performed, even though the copy-constructor of the data objects is not called. The copy process happens at machine code level.
Solution 2 - The move semantic
For this solution I have to mark the Move-Constructor of DataType
with noexcept
.
The key idea is not to treat the DataType[]
array as a std::vector<double>
.
Instead we treat the std::vector<double>
as a std::vector<DataType>
. We can
then move the data into this vector (without copying), send it to the function,
and move it back afterwards.
The data is not copied but moved std::vector
, which is faster. Also relevant for my case I can again take a just part from the initial data stored in my complex class.
Drawback with this solution I have to create an additional storage for the
moved data with the correct size.
#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];
}