Repository navigation
Expand file tree
/
Copy pathfunction.cpp
More file actions
160 lines (115 loc) · 4.21 KB
/
Copy pathfunction.cpp
File metadata and controls
160 lines (115 loc) · 4.21 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// # function
#include "common.hpp"
// TODO factor those dummy classes.
class Class {};
class Base {};
class BaseProtected {};
// Default args. C++ only.
// Creates several name mungled functions on the assembly code.
int DefaultArgs(int i, int j=0) { return i + j; }
// ERROR: Cannot use one argument as the default for the other.
//int DefaultArgs(float f, float f2=f) { return f + f2; }
// ERROR: no compound literals in c++
//void foo (int bar[] = (int[2]){0 ,1});
// Function overloading
void overload(int i){callStack.push_back("overload(int)");}
void overload(int i, int j){callStack.push_back("overload(int,int)");}
void overload(float i){callStack.push_back("overload(float)");}
void overload(float i, float j, float k=0.f){callStack.push_back("overload(float,float,float=)");}
// 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) {}
// Default arguments.
void defaultArgProto(int i=0);
void defaultArgProto(int i) {}
// BAD: usually not what you want
// since includers will not see the default version
void defaultArgDef(int i);
void defaultArgDef(int i=0) {}
// ERROR: default cannot go in declaration and definition
//void defaultArgBoth(int i=0);
//void defaultArgBoth(int i=0) {}
int def_no_argname(int) { return 1; }
int def_no_argname(int, int) { return 2; }
/*
# auto arguments
*/
/*ERROR: no you can't*/
/*
int func_auto(auto a){
++a;
return (int)a;
}
*/
int main() {
// # Overload
{
overload(1);
assert(callStack.back() == "overload(int)");
callStack.clear();
//base type conversions
{
overload(1.0f);
/*
ERROR
ambiguous overload(int) overload(float)
compiler does not know wether convert double to float or int
*/
//overload(1.0);
}
assert(callStack.back() == "overload(float)");
callStack.clear();
/*
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);
// # Default arguments
{
assert(DefaultArgs(1) == 1);
assert(DefaultArgs(1, 1) == 2);
}
/*
# default arguments for references
There seems to be no standard way of doing that without using extra memory / verbosity.
<http://stackoverflow.com/questions/2816293/passing-optional-parameter-by-reference-in-c>
*/
/*
# volatile overload
Functions that differ by `volatile` can be overloaded:
- http://stackoverflow.com/questions/10242578/volatile-overloading
*/
{}
}
/*
In C++, unlike in C, definitions can ommit argument names if they don't use those arguments!
This probably exists for method overridding.
*/
{
assert(def_no_argname(0) == 1);
assert(def_no_argname(0, 0) == 2);
}
}