-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexcpp20_.cpp
More file actions
52 lines (44 loc) · 962 Bytes
/
excpp20_.cpp
File metadata and controls
52 lines (44 loc) · 962 Bytes
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
// implement pre/post increment for a UDT.
// why is preincrement faster
// when can compilers optimize the postincrement
// postincrement is the one that takes an int postinc should return a
// const T so that t++++ doesn't work (it increments a temporary the
// second time)
//
#include "study.hpp"
struct T {
int j;
T() : j(0) { };
T& operator++() {
SHOW();
j += 1;
return *this;
}
const T operator++(int) {
SHOW();
T tmp = *this;
j += 1;
return tmp;
}
};
std::ostream& operator<<(std::ostream& os, const T& t)
{
return os << "[T " << t.j << "]";
}
int main()
{
{
T t;
std::cout << "postinc\n" << t << "\n";
std::cout << t++ << "\n";
std::cout << t++ << "\n";
// std::cout << t++++ << "\n"; this shouldn't compile
}
{
T t;
std::cout << "preinc\n" << t << "\n";
std::cout << ++t << "\n";
std::cout << ++t << "\n";
std::cout << ++++t << "\n"; // this is okay
}
}