0

I am using different types of data : say double *, double **, Eigen::Matrix< Index, Eigen::Dynamic, Eigen::Dynamic >, std::vector ...

I am using different codes, and they mix those types, so I would like to avoid copies of datas when passing from one type to another, e.g. say I have double * positions, and in another part of code I need std::vector vectorOfPositions, and I would like to avoid costly initialiazation of vectorOfPositions... Similarly, Eigen with the method Map, seems to offer a solution. I wonder if there is a general way of doing so ...

So thanks a lot for for the help. This std::span solution is attractive but the primary design of the algorithms I am using is not mine.

Some examples of the codes I was refering to :

typedef Eigen::Matrix<double,3,1> Vec3x;
typedef std::vector<Vec3xArray, Eigen::aligned_allocator<Vec3x> Vec3xArray;
typedef std::vector<Eigen::Vector3d> line;
struct point { double x; double y; double z;}; 
typedef struct point point;

point* m_points;
std::vector<double> m_positions;
Vec3xArray vertices;
line m_line;

Partial answer for std::vector< Type > to Type* : "The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array." ref. here.

Partial answer for Type* to std::vector : std::vector assumes ownership of the data -> this is not possible !

With Eigen, use the Map method to initialize, and data() to get a pointer to the data. See Eigen doc.

2
  • I don't quite understand your question, but do you need something like std::variant or std::any Commented May 7, 2021 at 9:04
  • "I have double * positions, and in another part of code I need std::vector vectorOfPositions, and I would like to avoid costly initialiazation of vectorOfPositions." — Some exemplary code would be useful. You also should specify which parts of code you can control. Can you modify it? Is it a library code? Commented May 7, 2021 at 9:15

1 Answer 1

3

Yes, there is a "general way of doing so". That is, to design algorithms such that they do not work with particular data structures (containers). Instead, they work with iterators, which basically abstract ranges of elements. You can find many examples in the C++ standard library.

You can also use classes like std::string_view or std::span in specific cases.

If some algorithm needs std::vector instead of a pair of iterators or std::span, it is IMO kind-of a design problem.

Sign up to request clarification or add additional context in comments.

4 Comments

OP said it's sometimes double** and sometimes double*. I don't think std::span can handle that.
@AyxanHaqverdili Why not? You can build a span of spans. If double** represents a 2D array, there is a problem that it is very unsafe to process it as is (it's more C than C++).
Yes, span can handle double** and double* but not at the same time. To me it looks like OP wants an argument that can be one of a closed set of types. This seems like a job for std::variant.
@AyxanHaqverdili I don't think so. They want to avoid copies. How would you copy data from double** to double*?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.