P'tit Ju
P'tit Ju

Reputation: 15

How to add std::vector with boost::proto?

I discovered boost::proto and I tried to add three std::vector with it as example. My objective is to have a behavior like Eigen vectors. I understand the error but I don't know how to solve it. I tried to use boost::proto::eval with a default context but it fails too because no operator + is defined between two std::vector. It is an example and I want to do the same for the operator & for example but I don't know then how to specify the behavior of the operator. Here is the code.

#include <vector>
 
#include <boost/proto/proto.hpp>
 
template<typename T>
struct is_terminal : boost::mpl::false_
{
};
 
template<>
struct is_terminal<std::vector<int> > : boost::mpl::true_
{
};
 
BOOST_PROTO_DEFINE_OPERATORS(is_terminal, boost::proto::default_domain)
 
int main()
{
    std::vector<int> const a{0, 1, 2, 3};
    std::vector<int> const b{4, 5, 6, 7};
    std::vector<int> const c{0, 0, 0, 0};
 
    auto const exprAdd{a + b + c};
    auto const exprAnd{a & b & c};
    std::vector<int> const d{exprAdd}; //no known conversion from proto expression and call of boost::proto:eval with a default context fails
    std::vector<int> const e{exprAnd}; //how to specify the behavior of operator & (like the operator + above I imagine)?

    return 0;
}

And the error is:

D:\programmation\cpp\TemplateExpression\main.cpp: In function 'int main()':
D:\programmation\cpp\TemplateExpression\main.cpp:25:37: error: no matching function for call to 'std::vector<int>::vector(<brace-enclosed initializer list>)'
   25 |     std::vector<int> const d{exprAdd};
      |                                     ^
In file included from D:/Programmes/msys64/ucrt64/include/c++/13.2.0/vector:66,
                 from D:\programmation\cpp\TemplateExpression\main.cpp:2:
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:704:9: note: candidate: 'template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with <template-parameter-2-2> = _InputIterator; _Tp = int; _Alloc = std::allocator<int>]'
  704 |         vector(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:704:9: note:   template argument deduction/substitution failed:
D:\programmation\cpp\TemplateExpression\main.cpp:25:37: note:   candidate expects 3 arguments, 1 provided
   25 |     std::vector<int> const d{exprAdd};
      |                                     ^
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:675:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>]'
  675 |       vector(initializer_list<value_type> __l,
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:675:43: note:   no known conversion for argument 1 from 'const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>&, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>' to 'std::initializer_list<int>'
  675 |       vector(initializer_list<value_type> __l,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:656:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, std::__type_identity_t<_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::__type_identity_t<_Alloc> = std::allocator<int>]'
  656 |       vector(vector&& __rv, const __type_identity_t<allocator_type>& __m)
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:656:7: note:   candidate expects 2 arguments, 1 provided
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:637:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::false_type) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>; std::false_type = std::integral_constant<bool, false>]'
  637 |       vector(vector&& __rv, const allocator_type& __m, false_type)
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:637:7: note:   candidate expects 3 arguments, 1 provided
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:632:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::true_type) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>; std::true_type = std::integral_constant<bool, true>]'
  632 |       vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:632:7: note:   candidate expects 3 arguments, 1 provided
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:621:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, std::__type_identity_t<_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::__type_identity_t<_Alloc> = std::allocator<int>]'
  621 |       vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:621:7: note:   candidate expects 2 arguments, 1 provided
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:617:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>]'
  617 |       vector(vector&&) noexcept = default;
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:617:14: note:   no known conversion for argument 1 from 'const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>&, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>' to 'std::vector<int>&&'
  617 |       vector(vector&&) noexcept = default;
      |              ^~~~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:598:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]'
  598 |       vector(const vector& __x)
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:598:28: note:   no known conversion for argument 1 from 'const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>&, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>' to 'const std::vector<int>&'
  598 |       vector(const vector& __x)
      |              ~~~~~~~~~~~~~~^~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:566:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(size_type, const value_type&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; size_type = long long unsigned int; value_type = int; allocator_type = std::allocator<int>]'
  566 |       vector(size_type __n, const value_type& __value,
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:566:7: note:   candidate expects 3 arguments, 1 provided
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:553:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; size_type = long long unsigned int; allocator_type = std::allocator<int>]'
  553 |       vector(size_type __n, const allocator_type& __a = allocator_type())
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:553:24: note:   no known conversion for argument 1 from 'const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>&, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>' to 'std::vector<int>::size_type' {aka 'long long unsigned int'}
  553 |       vector(size_type __n, const allocator_type& __a = allocator_type())
      |              ~~~~~~~~~~^~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:539:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>]'
  539 |       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:539:36: note:   no known conversion for argument 1 from 'const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::plus, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>&, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<const std::vector<int>&>, 0> >, 2>' to 'const std::vector<int>::allocator_type&' {aka 'const std::allocator<int>&'}
  539 |       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      |              ~~~~~~~~~~~~~~~~~~~~~~^~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:528:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]'
  528 |       vector() = default;
      |       ^~~~~~
D:/Programmes/msys64/ucrt64/include/c++/13.2.0/bits/stl_vector.h:528:7: note:   candidate expects 0 arguments, 1 provided

Maybe I have to create a vectori class that inherits from std::vector and create a constructor from an expression and so the behavior of the operators but I don't know how too even if reading the examples of the documentation.

Upvotes: -1

Views: 39

Answers (0)

Related Questions