How to reduce redundant code when adding new c++0x rvalue reference operator overloads

后端 未结 4 1527
不思量自难忘°
不思量自难忘° 2020-12-08 05:29

I am adding new operator overloads to take advantage of c++0x rvalue references, and I feel like I\'m producing a lot of redundant code.

I have a class, tree

4条回答
  •  被撕碎了的回忆
    2020-12-08 05:38

    You're supposed to define them as member functions, so that you don't have to overload on lvalue or rvalue as the primary unit (which is unnecessary anyway) That is,

    class Tree {
        Tree operator+ const (const Tree&);
        Tree operator+ const (Tree&&);
    };
    

    because the l or r valueness of the first is irrelevant. In addition, the compiler will automatically construct for you if that constructor is available. If tree constructs from double, then you can automatically use doubles here, and the double will be appropriately an rvalue. This is just two methods.

提交回复
热议问题