File: binops.yo

package info (click to toggle)
bobcat 6.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,960 kB
  • sloc: cpp: 18,954; fortran: 5,617; makefile: 2,787; sh: 659; perl: 401; ansic: 26
file content (143 lines) | stat: -rw-r--r-- 5,831 bytes parent folder | download | duplicates (3)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
includefile(include/header)

COMMENT(manpage, section, releasedate, archive, short name)
manpage(binops)(3bobcat)(_CurYrs_)(libbobcat-dev__CurVers_)
                    (Binary Operators)

manpagename(binops)(Template functions for class-type binary operators)

manpagesynopsis()
    bf(#include <utility>)nl()
    bf(#include <bobcat/typetrait>)nl()
    bf(#include <bobcat/binops>)nl()

manpagedescription()
    Classes can overload binary operators. A class named tt(Class) may
overload these binary operators to suit its own needs, allowing, e.g., two
tt(Class) type objects to be added after overloading tt(operator+). Operators
for the binary operators *, /, %, +, -, <<, >>, &, |, and ^ (in this man-page
they are generically indicated as the `tt(@)' operator) can be overloaded by
defining the tt(operator@) function.

    If a class supports copy construction and if it offers binary assignment
operators (i.e., it offers members of the form tt(operator@=)), then the
matching binary operators can all be implemented identically. The
em(move-aware) tt(Class &operator@(Class &&lhs, Class const &rhs)) is easily
implemented in terms of tt(operator@=) (note that the class itself doesn't
have to be `move-aware' to define this function). The move-aware binary
operator one requires a one line implementation, and as its
implementation never changes it could safely be defined tt(inline):
        verb(
Class operator@(Class &&lhs, Class const &rhs)
{
    return std::move(std::move(lhs) @= rhs);
}
        )
    The traditional binary operator can be implemented using its standard
form:
        verb(
Class operator@(Class const &lhs, Class const &rhs)
{
    Class tmp(lhs);
    tmp @= rhs;
    return tmp;
}
        )
    The implementation in tt(bobcat/binops) is slightly more complex as it
allows from lhs or rhs promotions.

    As the binary operators can all be implemented alike their definitions are
perfectly suited for templates: A class offering a particular tt(operator@=)
then automatically also offers the matching binary operators after including
tt(bobcat/binops). Since the binary function templates are not instantiated
until used their definitions can be processed by the compiler even if a class
implements only a subset of the available binary assignment operators.

manpagesection(NAMESPACE)

    The binary operator functions templates in tt(bobcat/binops) are em(not)
implemented in a particular namespace. This allows sources to include
tt(bobcat/binops) in multiple namespaces.

    If tt(bobcat/binops) is to be used in multiple namespaces then the include
safeguard (using the identifier tt(INCLUDED_BOBCAT_BINOPS_)) must be
suppressed between inclusions of tt(bobcat/binops) in different
namespaces.

    E.g., to make the binary operator function templates available in a source
file using the tt(namespace FBB) and in a source file using the default
namespace the following scheme can be used:
        verb(
#include <utility>              // ensure std::move is available
#include <bobcat/typetrait>     // required by binops

namespace MY_NAMESPACE
{
    #include <bobcat/binops>    // binary operators available in MY_NAMESPACE
}
#undef INCLUDED_BOBCAT_BINOPS_  // suppress the include guard

#include <bobcat/binops>        // read binops again so the binary
                                // operators can be used in the
                                // default namespace as well
    )

manpagesection(INHERITS FROM)
    -

manpagesection(OVERLOADED OPERATORS)
    The function templates in tt(bobcat/binops) implement all arithmetic
binary operators, both move-aware and the traditional binary operators,
expecting constant lvalue references. They can be used if the matching binary
assignment operators were implemented in the classes for which the templates
must be instantiated. The following operators are available:

Move-aware operators, using temporary objects for its left-hand side operands:
    itemization(
    itb(Class operator*(Class &&lhs, Class const &rhs))
    itb(Class operator/(Class &&lhs, Class const &rhs))
    itb(Class operator%(Class &&lhs, Class const &rhs))
    itb(Class operator+(Class &&lhs, Class const &rhs))
    itb(Class operator-(Class &&lhs, Class const &rhs))
    itb(Class operator<<(Class &&lhs, Class const &rhs))
    itb(Class operator>>(Class &&lhs, Class const &rhs))
    itb(Class operator&(Class &&lhs, Class const &rhs))
    itb(Class operator|(Class &&lhs, Class const &rhs))
    itb(Class operator^(Class &&lhs, Class const &rhs))
    )

`Traditional' operators, using lvalue references to constant objects
for its left-hand side operands:
    itemization(
    itb(Class operator*(Class const &lhs, Class const &rhs))
    itb(Class operator/(Class const &lhs, Class const &rhs))
    itb(Class operator%(Class const &lhs, Class const &rhs))
    itb(Class operator+(Class const &lhs, Class const &rhs))
    itb(Class operator-(Class const &lhs, Class const &rhs))
    itb(Class operator<<(Class const &lhs, Class const &rhs))
    itb(Class operator>>(Class const &lhs, Class const &rhs))
    itb(Class operator&(Class const &lhs, Class const &rhs))
    itb(Class operator|(Class const &lhs, Class const &rhs))
    itb(Class operator^(Class const &lhs, Class const &rhs))
    )
    The latter group of operators also support promotions.

manpagesection(EXAMPLE)
    verbinclude(../../binops/driver/driver.cc)

manpagefiles()
    em(bobcat/binops) - defines the binary operator function templates

manpageseealso()
    bf(bobcat/binopsbase)(3)
    bf(bobcat)(7)

manpagebugs()
    itemization(
    it() The header files tt(utility), defining tt(std::move), and
tt(bobcat/typetrait) are required by, but are not included by
tt(bobcat/binops). This was a design decision, see the bf(NAMESPACE)
section.
    )

includefile(include/trailer)