File: operators.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (33 lines) | stat: -rw-r--r-- 1,204 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
In section ref(OVERLOADBINARY) addition operators are implemented in terms of
a support member tt(add). This is less attractive when developing function
templates, as tt(add) is a private member, requiring us to provide friend
declarations for all function templates so they may access the private tt(add)
member.

At the end of section ref(OVERLOADBINARY) we saw that tt(add's) implementation
can be provided by tt(operator+=(Class const &rhs) &&). This operator may
thereupon be used when implementing the remaining addition operators:
        verb(    inline Binary &operator+=(Binary const &rhs) &
    {
        return *this = Binary{*this} += rhs;        
    }

    Binary operator+(Binary &&lhs, Binary const &rhs)
    {
        return std::move(lhs) += rhs;
    }

    Binary operator+(Binary const &lhs, Binary const &rhs)
    {
        return Binary{lhs} += rhs;
    })

In this implementation tt(add) is no longer required. The plain binary
operators are free functions, which supposedly can easily be converted to
function templates. E.g.,
        verb(    template <typename Binary>
    Binary operator+(Binary const &lhs, Binary const &rhs)
    {
        return Binary{lhs} += rhs;
    })