Skip to content

Commit 865ee50

Browse files
committed
[core] Avoid errors when using std::span backport from Python
The `vector<T> const&` constructor needs to be disabled if T is not a const type, because we don't want to create `span<T>` from `vector<T> const&`. The explicit disabling via SFINAE is necessary to this overload is not accidentally taken by cppyy, resulting in the failure of constructor wrapper code compilation. Avoids errors like these when compiling ROOT with C++17: ```txt In module 'Core': /home/rembserj/code/root/root_install/include/ROOT/span.hxx:208:27: error: cannot initialize a member subobject of type 'pointer' (aka 'double *') with an rvalue of type 'const double *' : length_(v.size()), data_(v.empty() ? nullptr : v.data()) ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ input_line_45:7:41: note: in instantiation of member function 'std::span<double>::span' requested here (*(std::span<double>**)ret) = new std::span<double>((const std::vector<double, std::allocator<double> >&)*(const std::vector<double, std::allocator<double> >*)args[0]); ^ 0 1 2 3 ``` Reproducer: ```Python import ROOT ROOT.gInterpreter.Declare(""" void calc_cumsum(std::size_t n, std::span<double> v) { for (int i = 0; i < n; i += 1) { std::cout << v[i] << std::endl; } } """) v = ROOT.std.vector["double"](list(range(4))) ROOT.calc_cumsum(len(v), v) ```
1 parent fd48a6d commit 865ee50

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

core/foundation/inc/ROOT/span.hxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ public:
204204
static_assert(N > 0, "Zero-length array is not permitted in ISO C++.");
205205
}
206206

207+
// Note:
208+
// This constructor needs to be disabled if T is not a const type, because we
209+
// don't want to create span<T> from vector<T> const&.
210+
// The explicit disabling via SFINAE is necessary to this overload is not
211+
// accidentally taken by cppyy, resulting in the failure of constructor
212+
// wrapper code compilation.
213+
template <class U = T, class = typename std::enable_if<std::is_const<U>::value>::type>
207214
/*implicit*/ span(std::vector<typename std::remove_cv<T>::type> const& v) noexcept
208215
: length_(v.size()), data_(v.empty() ? nullptr : v.data())
209216
{}

0 commit comments

Comments
 (0)