arrayfire / arrayfire/arrayfire

Create array from vector<long>

Open
#2,412 3 comments 0 reactions 0 assignees View on GitHub
discuss feature
Dominant language
C++
Stars
4.9k
Forks
555
Avg merge
1h 24m
Merged PRs (30d)
1

Description

Currently, this code will not link (using 3.6.1):

#include

int main() {
std::vector v{1, 2, 3};
auto a = af::array(v.size(), v.data());
}

/*
error: undefined reference to 'af::array::array(long long, long const*, af_source)'
clang-7.0: error: linker command failed with exit code 1 (use -v to see invocation)
*/

Indeed, I can't see any safe way to convert a `std::vector` to an `af::array` without copying the vector first. Trying something like this:

static_assert(sizeof(long) == sizeof(long long));
auto a = af::array(v.size(), reinterpret_cast(v.data()));

is unsafe due to strict aliasing rules. Same thing with using `af_create_array` or `af_write_array` if `long*` is passed.

On my platform `int64_t` is typedef'd to `long`, so I run into the same issue with `std::vector`. This is unfortunate since the correspondence between `int64_t` and `af::dtype::s64` is suggestive.

Is there any way around this issue, besides just copying the vector?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.