-
Notifications
You must be signed in to change notification settings - Fork 160
/
overload.cpp
120 lines (93 loc) · 3.17 KB
/
overload.cpp
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
/*
# Overload
*/
#include "common.hpp"
// TODO factor those dummy classes.
class Class {};
class Base {};
class BaseProtected {};
std::string overload(int i) { return "i"; }
std::string overload(float i) { return "f"; }
std::string overload(int i, int j) { return "ii"; }
std::string overload(float i, float j, float k = 0.f) { return "iff"; }
// OK even if return type is different all is decided at compile time.
//int overload(int i, int j, int k){return 1;}
// ERROR: conflict with int.
//void overload(const int i){}
// ERROR: cannot differentiate by output since output is used to decide if other parts of code make sense.
//int overload(){return 0;}
//float overload(){return 0.f;}
// ERROR: conflict with int int
//void overload(int i, int j=0){cout << "int int=";}
// BAD: compiles, but is useless to give a default,
// since when calling, caller is *forced* to give a value for j
// or wil get `call is ambiguous` compile time error
// because compiler cannot decide between
// here the default arg can be usefull for a call of type float float.
//void overload(float i, float j=1){cout << "float float=";}
// TODO: why does this compile, that is, how not to make an ambiguous call with overload(int).
void overloadValAddr(const int i) {}
void overloadValAddr(const int& i) {}
void overloadBase(Base b) {}
void overloadBase(BaseProtected b) {}
int main() {
assert(overload(1) == "i");
//base type conversions
{
assert(overload(1.0f) == "f");
/*
ERROR: ambiguous overload(int) overload(float)
Compiler does not know wether convert double to float or int.
*/
//overload(1.0);
}
/*
ERROR: ambiguous
should compiler coverts to Base or BaseProtected? Both are possible via the default copy constructors.
*/
{
Class cOverload;
//overloadBase(cOverload);
}
// ERROR: ambiguous
//i=4;
//overloadValAddr(i);
/*
# volatile overload
Functions that differ by `volatile` can be overloaded:
- http://stackoverflow.com/questions/10242578/volatile-overloading
*/
{}
/*
Function pointer: decided by the typecast.
http://stackoverflow.com/questions/2942426/how-to-specify-a-pointer-to-an-overloaded-function
*/
{
// Variable.
{
std::string (*fi)(int) = overload;
std::string (*ff)(float) = overload;
}
}
// # const function overload
// http://stackoverflow.com/questions/251159/what-is-the-use-of-const-overloading-in-c
// http://stackoverflow.com/questions/5103543/how-does-overloading-of-const-and-non-const-functions-work/41274591#41274591
{
class C {
public:
int f() { return 1; }
float f() const { return 1.5; }
};
// Non const.
C c;
assert(c.f() == 1);
// Convert variable const at compile time.
assert(const_cast<const C&>(c).f() == 1.5);
// Same as above but with an explicit reference.
const C& d = c;
assert(d.f() == 1.5);
// Analogous but with a new const object from the start.
const C e;
assert(d.f() == 1.5);
}
}