Treeman
Treeman

Reputation: 106

Creating std::span from pybind array

void createSpanfromNumpy(pybind11::array_t<int>& inputA, std::vector<int>& inputB)
{
    auto addressA = inputA.data();

    auto addressB = inputB.data();

    std::span<int> testSpanA{ inputA.data(), inputA.size()};


    std::span<int> testSpanB{ inputB.data(), inputB.size() };

    //do stuff with span

}

Trying to grab references to the memory used in a numpy array and pass them around in C++ for convenience. In the example code above it works fine for a std::vector but when trying to create a span from numpy array (inputA), this compiler error is produced:

Error   C2440   'initializing': cannot convert from 'initializer list' to 'std::span<int,4294967295>'   numpytoSpan 

What is it about the pointer coming out of the array_t.data() call which doesn't work?

Upvotes: 0

Views: 190

Answers (1)

Artyer
Artyer

Reputation: 40891

inputA.data() is a const int*.

Use a std::span<const int> or use pybind11::array_t::mutable_data.

Upvotes: 2

Related Questions