c++ - Expression template operator overloading problem with std::vector -
i'm working on numerical library uses expression templates. unfortunately encountered problem operator overloads. consider following stripped down example.
#include <vector>  namespace test {     class test {};      template<class a, class b>     class testexpr {};      template<class a, class b>     testexpr<a, b>     operator-(a a, b b)     {         return testexpr<a, b>();     } }  test::test stuff(std::vector<test::test> &v) { return v.back(); }  int main() { }   which gives following error message when compiling gcc 4.4.3 or clang 2.8:
in file included eir_test.cc:2: in file included /usr/include/c++/4.4/vector:64: /usr/include/c++/4.4/bits/stl_vector.h:696:16: error: indirection requires pointer operand       ('testexpr<__gnu_cxx::__normal_iterator<test::test *, std::vector<test::test, std::allocator<test::test> > >, int>' invalid)       { return *(end() - 1); }                ^~~~~~~~~~~~ eir_test.cc:21:12: note: in instantiation of member function 'std::vector<test::test, std::allocator<test::test> >::back' requested here     return v.back();            ^ 1 error generated.   for reason compilers lookup test namespace , find general operator. used form traits magic reduce number of version had make operator. should accept 4 different datatypes (including double , int) lead lot of different combinations.
is there way make work without spelling out combinations every operator?
this because end() returns type class template specialization has 1 argument of type test::test *. when operator- applied in expression end() - 1, argument dependent lookup looks also in namespace of test::test. finds operator- , passes iterator , int. 
you fix not accepting , types arguments. example, try accepting (testexpr<a1, b1>, testexpr<a2, b2>) instead. show combinations, possibly there way cut them down using way formulate them?
in opinion, implementation acts way should non-conforming (i think disgusting though). because doing iterator - 1 specified yield iterator previous element , must not crazy that, think. 1 way declare operator non-template accepting iterator type , integer argument (which of iterator's difference_type) directly. way version should preferred. 
Comments
Post a Comment