{"action":"created","issue":{"url":"https://api.github.com/repos/QuantStack/xtensor/issues/859","repository_url":"https://api.github.com/repos/QuantStack/xtensor","labels_url":"https://api.github.com/repos/QuantStack/xtensor/issues/859/labels{/name}","comments_url":"https://api.github.com/repos/QuantStack/xtensor/issues/859/comments","events_url":"https://api.github.com/repos/QuantStack/xtensor/issues/859/events","html_url":"https://github.com/QuantStack/xtensor/issues/859","id":324623660,"number":859,"title":"Compile error with xt::flatten using xt::xcontainer","user":{"login":"benbovy","id":4160723,"avatar_url":"https://avatars0.githubusercontent.com/u/4160723?v=4","gravatar_id":"","url":"https://api.github.com/users/benbovy","html_url":"https://github.com/benbovy","followers_url":"https://api.github.com/users/benbovy/followers","following_url":"https://api.github.com/users/benbovy/following{/other_user}","gists_url":"https://api.github.com/users/benbovy/gists{/gist_id}","starred_url":"https://api.github.com/users/benbovy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benbovy/subscriptions","organizations_url":"https://api.github.com/users/benbovy/orgs","repos_url":"https://api.github.com/users/benbovy/repos","events_url":"https://api.github.com/users/benbovy/events{/privacy}","received_events_url":"https://api.github.com/users/benbovy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-19T11:44:02Z","updated_at":"2018-05-19T12:10:47Z","closed_at":null,"author_association":"NONE","body":"I'm wondering why the following code gives me a compile error:\r\n\r\n```cpp\r\ntemplate<class D>\r\nvoid test_flatten(const xt::xcontainer<D>& arr) {\r\n    const auto arr_flat = xt::flatten(arr);\r\n    if(arr_flat(3))\r\n    { \r\n        std::cout << \"element is true\";\r\n    }\r\n}\r\n\r\nxt::xtensor<bool, 2> myarr {{false, false}, {true, true}};\r\n\r\ntest_flatten(my_arr);\r\n```\r\n\r\n<details>\r\n<summary>Error details</summary>\r\n\r\n```\r\ninput_line_66:4:8: error: value of type 'const xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &>::value_type'\r\n      (aka 'const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >') is not contextually convertible to\r\n      'bool'\r\n    if(arr_flat(3))\r\n       ^~~~~~~~~~~\r\ninput_line_67:2:2: note: in instantiation of function template specialization 'test_flatten4<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >'\r\n      requested here\r\n test_flatten4(arr);\r\n ^\r\nIn file included from input_line_7:1:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xtensor.hpp:19:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xcontainer.hpp:23:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xmath.hpp:22:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xaccumulator.hpp:18:\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrides.hpp:105:63: error: no member named 'cbegin' in 'std::__1::array<unsigned long, 0>'\r\n        return static_cast<std::size_t>(std::accumulate(shape.cbegin(), shape.cend(), size_type(1), std::multiplies<size_type>()));\r\n                                                        ~~~~~ ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:785:26: note: in instantiation of function template specialization 'xt::compute_size<std::__1::array<unsigned long, 0> >' requested here\r\n                m_size = compute_size(m_e.shape());\r\n                         ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:100:20: note: in instantiation of member function 'xt::detail::flat_expression_adaptor<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &> >::flat_expression_adaptor' requested here\r\n            return flat_expression_adaptor<E>(e);\r\n                   ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:343:26: note: in instantiation of function template specialization 'xt::detail::get_flat_storage<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, nullptr>' requested here\r\n          m_data(detail::get_flat_storage<CT>(m_e)),\r\n                         ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:1227:20: note: in instantiation of function template specialization 'xt::xstrided_view<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, std::__1::array<unsigned long, 1>, xt::detail::flat_expression_adaptor<xt::xscalar<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &> > >::xstrided_view<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &>' requested here\r\n            return view_type(std::forward<E>(e),\r\n                   ^\r\ninput_line_67:2:2: note: in instantiation of function template specialization 'test_flatten4<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >'\r\n      requested here\r\n test_flatten4(arr);\r\n ^\r\nIn file included from input_line_7:1:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xtensor.hpp:19:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xcontainer.hpp:23:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xmath.hpp:22:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xaccumulator.hpp:18:\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrides.hpp:189:46: error: no member named 'size' in 'std::__1::array<unsigned long, 0>'\r\n                for (std::size_t i = strides.size(); i != 0; --i)\r\n                                     ~~~~~~~ ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrides.hpp:212:24: note: in instantiation of function template specialization 'xt::detail::compute_strides<std::__1::array<unsigned long, 0>, std::__1::array<unsigned long, 0>, nullptr_t>' requested here\r\n        return detail::compute_strides(shape, l, strides, nullptr);\r\n                       ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:789:17: note: in instantiation of function template specialization 'xt::compute_strides<std::__1::array<unsigned long, 0>, std::__1::array<unsigned long, 0> >' requested here\r\n                compute_strides(m_e.shape(), m_layout, m_strides);\r\n                ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:100:20: note: in instantiation of member function 'xt::detail::flat_expression_adaptor<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &> >::flat_expression_adaptor' requested here\r\n            return flat_expression_adaptor<E>(e);\r\n                   ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:343:26: note: in instantiation of function template specialization 'xt::detail::get_flat_storage<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, nullptr>' requested here\r\n          m_data(detail::get_flat_storage<CT>(m_e)),\r\n                         ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:1227:20: note: in instantiation of function template specialization 'xt::xstrided_view<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, std::__1::array<unsigned long, 1>, xt::detail::flat_expression_adaptor<xt::xscalar<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &> > >::xstrided_view<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &>' requested here\r\n            return view_type(std::forward<E>(e),\r\n                   ^\r\ninput_line_67:2:2: note: in instantiation of function template specialization 'test_flatten4<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >'\r\n      requested here\r\n test_flatten4(arr);\r\n ^\r\nIn file included from input_line_8:1:\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:809:44: error: no member named 'cbegin' in 'std::__1::array<unsigned long, 0>'\r\n                return m_e.element(m_index.cbegin(), m_index.cend());\r\n                                   ~~~~~~~ ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:518:16: note: in instantiation of member function 'xt::detail::flat_expression_adaptor<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &> >::operator[]' requested here\r\n        return m_data[index];\r\n               ^\r\ninput_line_66:4:8: note: in instantiation of function template specialization 'xt::xstrided_view<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, std::__1::array<unsigned long, 1>, xt::detail::flat_expression_adaptor<xt::xscalar<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &> > >::operator()<int>' requested here\r\n    if(arr_flat(3))\r\n       ^\r\ninput_line_67:2:2: note: in instantiation of function template specialization 'test_flatten4<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >'\r\n      requested here\r\n test_flatten4(arr);\r\n ^\r\nIn file included from input_line_7:1:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xtensor.hpp:19:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xcontainer.hpp:23:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xmath.hpp:22:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xaccumulator.hpp:18:\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrides.hpp:249:54: error: no member named 'size' in 'std::__1::array<unsigned long, 0>'\r\n            S result = xtl::make_sequence<S>(strides.size(), 0);\r\n                                             ~~~~~~~ ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:808:35: note: in instantiation of function template specialization 'xt::detail::unravel_noexcept<std::__1::array<unsigned long, 0> >' requested here\r\n                m_index = detail::unravel_noexcept(idx, m_strides, m_layout);\r\n                                  ^\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrided_view.hpp:518:16: note: in instantiation of member function 'xt::detail::flat_expression_adaptor<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &> >::operator[]' requested here\r\n        return m_data[index];\r\n               ^\r\ninput_line_66:4:8: note: in instantiation of function template specialization 'xt::xstrided_view<xt::xscalar<const xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2,\r\n      xt::layout_type::row_major, xt::xtensor_expression_tag> > &>, std::__1::array<unsigned long, 1>, xt::detail::flat_expression_adaptor<xt::xscalar<const\r\n      xt::xcontainer<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> > &> > >::operator()<int>' requested here\r\n    if(arr_flat(3))\r\n       ^\r\ninput_line_67:2:2: note: in instantiation of function template specialization 'test_flatten4<xt::xtensor_container<xt::uvector<bool, std::__1::allocator<bool> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag> >'\r\n      requested here\r\n test_flatten4(arr);\r\n ^\r\nIn file included from input_line_7:1:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xtensor.hpp:19:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xcontainer.hpp:23:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xmath.hpp:22:\r\nIn file included from /Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xaccumulator.hpp:18:\r\n/Users/bbovy/miniconda3/envs/fastscapelib/include/xtensor/xstrides.hpp:252:51: error: no member named 'size' in 'std::__1::array<unsigned long, 0>'\r\n                for (size_type i = 0; i < strides.size(); ++i)\r\n```\r\n\r\n</details>\r\n</br>\r\n\r\nUsing ``xt::xexpression`` instead works:\r\n\r\n```cpp\r\ntemplate<class D>\r\nvoid test_flatten(const xt::xexpression<D>& arr) {\r\n    const D& arr_ = arr.derived_cast();\r\n    const auto arr_flat = xt::flatten(arr_);\r\n    if(arr_flat(3))\r\n    { \r\n        std::cout << \"element is true\";\r\n    }\r\n}\r\n```\r\n\r\nAs a side note: the latter example seems to be the pattern that I see more often in codes that are based on xtensor. However, in my use case (https://github.com/fastscape-lem/fastscapelib), I'm more interested in using xtensor for its extensible n-d array container than for its lazy expression system (I don't really need the later). My functions often have +4 array arguments and I find cumbersome having to add lines like ``const D& arr_ = arr.derived_cast();`` for each of these arguments in the function body. \r\n"},"comment":{"url":"https://api.github.com/repos/QuantStack/xtensor/issues/comments/390400825","html_url":"https://github.com/QuantStack/xtensor/issues/859#issuecomment-390400825","issue_url":"https://api.github.com/repos/QuantStack/xtensor/issues/859","id":390400825,"user":{"login":"SylvainCorlay","id":2397974,"avatar_url":"https://avatars0.githubusercontent.com/u/2397974?v=4","gravatar_id":"","url":"https://api.github.com/users/SylvainCorlay","html_url":"https://github.com/SylvainCorlay","followers_url":"https://api.github.com/users/SylvainCorlay/followers","following_url":"https://api.github.com/users/SylvainCorlay/following{/other_user}","gists_url":"https://api.github.com/users/SylvainCorlay/gists{/gist_id}","starred_url":"https://api.github.com/users/SylvainCorlay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SylvainCorlay/subscriptions","organizations_url":"https://api.github.com/users/SylvainCorlay/orgs","repos_url":"https://api.github.com/users/SylvainCorlay/repos","events_url":"https://api.github.com/users/SylvainCorlay/events{/privacy}","received_events_url":"https://api.github.com/users/SylvainCorlay/received_events","type":"User","site_admin":false},"created_at":"2018-05-19T12:10:47Z","updated_at":"2018-05-19T12:10:47Z","author_association":"MEMBER","body":"Responding from my phone so I could not double check.\r\n\r\nThe issue is that xcontainer is not an xexpression so it is understood by flatten as a scalar, and wrapped into an xscalar... That is the reason why it cannot work with xcontainer...\r\n\r\nIf you make the function a free template and enable it on xcontainers, you would not need the derived cast in the implementation.\r\n\r\nBy the way, fastscape-lem looks really cool!"}}
{"actor":{"display_login":"SylvainCorlay"}}