How do boost operators work?

前端 未结 1 1478
离开以前
离开以前 2021-02-04 04:42

boost::operators automatically defines operators like + based on manual implementations like += which is very useful. To generate those op

相关标签:
1条回答
  • 2021-02-04 05:08

    There's a big multiple inheritance chain, at the top of which there are a number of classes that implement the operators, but do so as friend functions, thus placing them in the enclosing namespace rather than as members of the class.

    For example, the final implementation of operator+ becomes:

    template <class T, class U, class B = ::boost::detail::empty_base<T> >
    struct addable2 : B
    {                                                                  
      friend T operator +( T lhs, const U& rhs ) { return lhs += rhs; }
      friend T operator +( const U& lhs, T rhs ) { return rhs += lhs; }
    };
    
    0 讨论(0)
提交回复
热议问题