Let me preface by saying that I have a background in Java and Python but not in any C-variants. I am trying to understand this piece of code that I came across.
extern "C" float wrapped(const unsigned long long* arr, const int len_arr) {
std::vector<unsigned long long> arr_vec(arr,arr+len_arr);
return SomeObj(arr_vec);
}
It is part of a cpp file, and is intended to call other C++ code (SomeObj
) through this C wrapper to make it accessible in Python. As this can be quite a pain to work well cross-platform, I wish to rewrite the C++ code to Cython, as some sort of exercise. The code itself is not too elaborate. But this C-wrapper confuses me.
I understand that there are multiple ways to initialize a vector in C++. In this case, arr_vec(arr,arr+len_arr)
I thought that it would be a fill-constructor -- but that can't be right. In a fill-constructor, the first item should be n
to indicate the size of the vector. But here the first item is the array, not the int. The second part that confuses me is that the second argument is summing an int to an array of longs. I know from Python that in numpy you can sum an int to an array, where arr+1 would add 1 to all items in an array. But I figured that such operations of mixing types was not possible in C-variants. Reading this code, I must be wrong.
So, in summary:
- what kind of vector constructor is being used here? How do the given arguments initialize a vector?
- can you just sum arrays and ints in C++? Is it similar to numpy in that respect?