xtensor-stack / xtensor-python

Python bindings for xtensor
BSD 3-Clause "New" or "Revised" License
347 stars 58 forks source link

Casting error in xview.hpp #167

Closed stuarteberg closed 6 years ago

stuarteberg commented 6 years ago

I'm having trouble building a project of mine after upgrading xtensor and xtensor-python. I think I've reduced it to the following test program, which compiles successfully (with clang) when I use xtensor-python 0.19.1 (xtensor 0.16.4), but fails to compile when I upgrade to xtensor-python 0.20.0 (xtensor 0.17.4).

If you can tell me what I'm doing wrong, I'd greatly appreciate it!

#include "xtensor/xarray.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xeval.hpp"

#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"

int main()
{
    xt::pyarray<int> arr = xt::zeros<int>({10});
    auto v = xt::view(arr, xt::all());
    xt::eval(v);

    return 0;
}

The error is:

/miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1419:13: error: non-constant-expression cannot be narrowed from type 'unsigned long' to
      'value_type' (aka 'long') in initializer list [-Wc++11-narrowing]
  ...(static_cast<std::ptrdiff_t>(xt::step_size(std::get<integral_skip<S...>(I)>(m_slices), 1)) * m_e.strides()[integral_skip<S...>(I) - newaxis_count_before<S...>(integral_skip<S...>(I))])...
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Click for compiler version and full error ``` $ clang --version Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.7.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin $ clang++ -std=c++14 -I${CONDA_PREFIX}/include -I ${CONDA_PREFIX}/include/python3.6m -I${CONDA_PREFIX}/lib/python3.6/site-packages/numpy/core/include -L${CONDA_PREFIX}/lib -lpython3.6m -Wl,-rpath,${CONDA_PREFIX}/lib test.cpp In file included from test.cpp:2: /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1419:13: error: non-constant-expression cannot be narrowed from type 'unsigned long' to 'value_type' (aka 'long') in initializer list [-Wc++11-narrowing] ...(static_cast(xt::step_size(std::get(I)>(m_slices), 1)) * m_e.strides()[integral_skip(I) - newaxis_count_before(integral_skip(I))])... ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1431:30: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::compute_strides_impl<0>' requested here auto slice_strides = compute_strides_impl(std::make_index_sequence()); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1197:13: note: in instantiation of member function 'xt::xview &, xt::xall >::compute_strides' requested here compute_strides(std::integral_constant{}); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1319:46: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::strides >' requested here return str.size() == self(this)->strides().size() && ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:215:63: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::is_trivial_broadcast, true> >' requested here (e1.layout() != layout_type::dynamic && e2.is_trivial_broadcast(e1.strides())); ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:316:53: note: in instantiation of function template specialization 'xt::detail::is_trivial_broadcast >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>, xt::xview &, xt::xall > >' requested here bool trivial_broadcast = trivial && detail::is_trivial_broadcast(de1, de2); ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:339:20: note: (skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all) base_type::assign_data(e1, e2, trivial_broadcast); ^ /miniforge/envs/test-xtensor/include/xtensor/xsemantic.hpp:587:13: note: in instantiation of function template specialization 'xt::assign_xexpression >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>, xt::xview &, xt::xall > >' requested here xt::assign_xexpression(*this, e); ^ /miniforge/envs/test-xtensor/include/xtensor/xsemantic.hpp:453:37: note: in instantiation of function template specialization 'xt::xcontainer_semantic >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag> >::assign_xexpression &, xt::xall > >' requested here return this->derived_cast().assign_xexpression(e); ^ /miniforge/envs/test-xtensor/include/xtensor/xarray.hpp:439:24: note: in instantiation of function template specialization 'xt::xsemantic_base >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag> >::assign &, xt::xall > >' requested here semantic_base::assign(e); ^ /miniforge/envs/test-xtensor/include/xtensor/xeval.hpp:52:16: note: in instantiation of function template specialization 'xt::xarray_container >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>::xarray_container &, xt::xall > >' requested here return xarray(std::forward(t)); ^ test.cpp:12:9: note: in instantiation of function template specialization 'xt::eval &, xt::xall > &, xt::xview &, xt::xall > >' requested here xt::eval(v); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1419:13: note: insert an explicit cast to silence this issue ...(static_cast(xt::step_size(std::get(I)>(m_slices), 1)) * m_e.strides()[integral_skip(I) - newaxis_count_before(integral_skip(I))])... ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ static_cast( ) /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1240:13: error: non-constant-expression cannot be narrowed from type 'unsigned long' to 'value_type' (aka 'long') in initializer list [-Wc++11-narrowing] (static_cast(xt::value(std::get(m_slices), 0)) * m_e.strides()[I - newaxis_count_before(I)])... ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1445:25: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::data_offset_impl<0>' requested here m_data_offset = data_offset_impl(std::make_index_sequence()); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1197:13: note: in instantiation of member function 'xt::xview &, xt::xall >::compute_strides' requested here compute_strides(std::integral_constant{}); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1319:46: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::strides >' requested here return str.size() == self(this)->strides().size() && ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:215:63: note: in instantiation of function template specialization 'xt::xview &, xt::xall >::is_trivial_broadcast, true> >' requested here (e1.layout() != layout_type::dynamic && e2.is_trivial_broadcast(e1.strides())); ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:316:53: note: in instantiation of function template specialization 'xt::detail::is_trivial_broadcast >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>, xt::xview &, xt::xall > >' requested here bool trivial_broadcast = trivial && detail::is_trivial_broadcast(de1, de2); ^ /miniforge/envs/test-xtensor/include/xtensor/xassign.hpp:339:20: note: (skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all) base_type::assign_data(e1, e2, trivial_broadcast); ^ /miniforge/envs/test-xtensor/include/xtensor/xsemantic.hpp:587:13: note: in instantiation of function template specialization 'xt::assign_xexpression >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>, xt::xview &, xt::xall > >' requested here xt::assign_xexpression(*this, e); ^ /miniforge/envs/test-xtensor/include/xtensor/xsemantic.hpp:453:37: note: in instantiation of function template specialization 'xt::xcontainer_semantic >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag> >::assign_xexpression &, xt::xall > >' requested here return this->derived_cast().assign_xexpression(e); ^ /miniforge/envs/test-xtensor/include/xtensor/xarray.hpp:439:24: note: in instantiation of function template specialization 'xt::xsemantic_base >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag> >::assign &, xt::xall > >' requested here semantic_base::assign(e); ^ /miniforge/envs/test-xtensor/include/xtensor/xeval.hpp:52:16: note: in instantiation of function template specialization 'xt::xarray_container >, xt::layout_type::row_major, xt::svector, true>, xt::xtensor_expression_tag>::xarray_container &, xt::xall > >' requested here return xarray(std::forward(t)); ^ test.cpp:12:9: note: in instantiation of function template specialization 'xt::eval &, xt::xall > &, xt::xview &, xt::xall > >' requested here xt::eval(v); ^ /miniforge/envs/test-xtensor/include/xtensor/xview.hpp:1240:13: note: insert an explicit cast to silence this issue (static_cast(xt::value(std::get(m_slices), 0)) * m_e.strides()[I - newaxis_count_before(I)])... ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ static_cast( ) 2 errors generated. ```
JohanMabille commented 6 years ago

Nothing wrong on your side, that's a bug in xtensor-python. I'm working on a fix.

JohanMabille commented 6 years ago

We have released 0.20.1 with a fix for your issue.

stuarteberg commented 6 years ago

Thanks!