tmp/tmpxlwmmhbl/{from.md → to.md}
RENAMED
|
@@ -1,176 +0,0 @@
|
|
| 1 |
-
## Overloadable declarations <a id="over.load">[[over.load]]</a>
|
| 2 |
-
|
| 3 |
-
Not all function declarations can be overloaded. Those that cannot be
|
| 4 |
-
overloaded are specified here. A program is ill-formed if it contains
|
| 5 |
-
two such non-overloadable declarations in the same scope.
|
| 6 |
-
|
| 7 |
-
[*Note 1*: This restriction applies to explicit declarations in a
|
| 8 |
-
scope, and between such declarations and declarations made through a
|
| 9 |
-
*using-declaration* [[namespace.udecl]]. It does not apply to sets of
|
| 10 |
-
functions fabricated as a result of name lookup (e.g., because of
|
| 11 |
-
*using-directive*s) or overload resolution (e.g., for operator
|
| 12 |
-
functions). — *end note*]
|
| 13 |
-
|
| 14 |
-
Certain function declarations cannot be overloaded:
|
| 15 |
-
|
| 16 |
-
- Function declarations that differ only in the return type, the
|
| 17 |
-
exception specification [[except.spec]], or both cannot be overloaded.
|
| 18 |
-
- Member function declarations with the same name, the same
|
| 19 |
-
parameter-type-list [[dcl.fct]], and the same trailing
|
| 20 |
-
*requires-clause* (if any) cannot be overloaded if any of them is a
|
| 21 |
-
`static` member function declaration [[class.static]]. Likewise,
|
| 22 |
-
member function template declarations with the same name, the same
|
| 23 |
-
parameter-type-list, the same trailing *requires-clause* (if any), and
|
| 24 |
-
the same *template-head* cannot be overloaded if any of them is a
|
| 25 |
-
`static` member function template declaration. The types of the
|
| 26 |
-
implicit object parameters constructed for the member functions for
|
| 27 |
-
the purpose of overload resolution [[over.match.funcs]] are not
|
| 28 |
-
considered when comparing parameter-type-lists for enforcement of this
|
| 29 |
-
rule. In contrast, if there is no `static` member function declaration
|
| 30 |
-
among a set of member function declarations with the same name, the
|
| 31 |
-
same parameter-type-list, and the same trailing *requires-clause* (if
|
| 32 |
-
any), then these member function declarations can be overloaded if
|
| 33 |
-
they differ in the type of their implicit object parameter.
|
| 34 |
-
\[*Example 1*:
|
| 35 |
-
The following illustrates this distinction:
|
| 36 |
-
``` cpp
|
| 37 |
-
class X {
|
| 38 |
-
static void f();
|
| 39 |
-
void f(); // error
|
| 40 |
-
void f() const; // error
|
| 41 |
-
void f() const volatile; // error
|
| 42 |
-
void g();
|
| 43 |
-
void g() const; // OK: no static g
|
| 44 |
-
void g() const volatile; // OK: no static g
|
| 45 |
-
};
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
— *end example*]
|
| 49 |
-
- Member function declarations with the same name, the same
|
| 50 |
-
parameter-type-list [[dcl.fct]], and the same trailing
|
| 51 |
-
*requires-clause* (if any), as well as member function template
|
| 52 |
-
declarations with the same name, the same parameter-type-list, the
|
| 53 |
-
same trailing *requires-clause* (if any), and the same
|
| 54 |
-
*template-head*, cannot be overloaded if any of them, but not all,
|
| 55 |
-
have a *ref-qualifier* [[dcl.fct]].
|
| 56 |
-
\[*Example 2*:
|
| 57 |
-
``` cpp
|
| 58 |
-
class Y {
|
| 59 |
-
void h() &;
|
| 60 |
-
void h() const &; // OK
|
| 61 |
-
void h() &&; // OK, all declarations have a ref-qualifier
|
| 62 |
-
void i() &;
|
| 63 |
-
void i() const; // error: prior declaration of i has a ref-qualifier
|
| 64 |
-
};
|
| 65 |
-
```
|
| 66 |
-
|
| 67 |
-
— *end example*]
|
| 68 |
-
|
| 69 |
-
[*Note 2*:
|
| 70 |
-
|
| 71 |
-
As specified in [[dcl.fct]], function declarations that have equivalent
|
| 72 |
-
parameter declarations and *requires-clause*s, if any
|
| 73 |
-
[[temp.constr.decl]], declare the same function and therefore cannot be
|
| 74 |
-
overloaded:
|
| 75 |
-
|
| 76 |
-
- Parameter declarations that differ only in the use of equivalent
|
| 77 |
-
typedef “types” are equivalent. A `typedef` is not a separate type,
|
| 78 |
-
but only a synonym for another type [[dcl.typedef]].
|
| 79 |
-
\[*Example 3*:
|
| 80 |
-
``` cpp
|
| 81 |
-
typedef int Int;
|
| 82 |
-
|
| 83 |
-
void f(int i);
|
| 84 |
-
void f(Int i); // OK: redeclaration of f(int)
|
| 85 |
-
void f(int i) { ... }
|
| 86 |
-
void f(Int i) { ... } // error: redefinition of f(int)
|
| 87 |
-
```
|
| 88 |
-
|
| 89 |
-
— *end example*]
|
| 90 |
-
Enumerations, on the other hand, are distinct types and can be used to
|
| 91 |
-
distinguish overloaded function declarations.
|
| 92 |
-
\[*Example 4*:
|
| 93 |
-
``` cpp
|
| 94 |
-
enum E { a };
|
| 95 |
-
|
| 96 |
-
void f(int i) { ... }
|
| 97 |
-
void f(E i) { ... }
|
| 98 |
-
```
|
| 99 |
-
|
| 100 |
-
— *end example*]
|
| 101 |
-
- Parameter declarations that differ only in a pointer `*` versus an
|
| 102 |
-
array `[]` are equivalent. That is, the array declaration is adjusted
|
| 103 |
-
to become a pointer declaration [[dcl.fct]]. Only the second and
|
| 104 |
-
subsequent array dimensions are significant in parameter types
|
| 105 |
-
[[dcl.array]].
|
| 106 |
-
\[*Example 5*:
|
| 107 |
-
``` cpp
|
| 108 |
-
int f(char*);
|
| 109 |
-
int f(char[]); // same as f(char*);
|
| 110 |
-
int f(char[7]); // same as f(char*);
|
| 111 |
-
int f(char[9]); // same as f(char*);
|
| 112 |
-
|
| 113 |
-
int g(char(*)[10]);
|
| 114 |
-
int g(char[5][10]); // same as g(char(*)[10]);
|
| 115 |
-
int g(char[7][10]); // same as g(char(*)[10]);
|
| 116 |
-
int g(char(*)[20]); // different from g(char(*)[10]);
|
| 117 |
-
```
|
| 118 |
-
|
| 119 |
-
— *end example*]
|
| 120 |
-
- Parameter declarations that differ only in that one is a function type
|
| 121 |
-
and the other is a pointer to the same function type are equivalent.
|
| 122 |
-
That is, the function type is adjusted to become a pointer to function
|
| 123 |
-
type [[dcl.fct]].
|
| 124 |
-
\[*Example 6*:
|
| 125 |
-
``` cpp
|
| 126 |
-
void h(int());
|
| 127 |
-
void h(int (*)()); // redeclaration of h(int())
|
| 128 |
-
void h(int x()) { } // definition of h(int())
|
| 129 |
-
void h(int (*x)()) { } // error: redefinition of h(int())
|
| 130 |
-
```
|
| 131 |
-
|
| 132 |
-
— *end example*]
|
| 133 |
-
- Parameter declarations that differ only in the presence or absence of
|
| 134 |
-
`const` and/or `volatile` are equivalent. That is, the `const` and
|
| 135 |
-
`volatile` type-specifiers for each parameter type are ignored when
|
| 136 |
-
determining which function is being declared, defined, or called.
|
| 137 |
-
\[*Example 7*:
|
| 138 |
-
``` cpp
|
| 139 |
-
typedef const int cInt;
|
| 140 |
-
|
| 141 |
-
int f (int);
|
| 142 |
-
int f (const int); // redeclaration of f(int)
|
| 143 |
-
int f (int) { ... } // definition of f(int)
|
| 144 |
-
int f (cInt) { ... } // error: redefinition of f(int)
|
| 145 |
-
```
|
| 146 |
-
|
| 147 |
-
— *end example*]
|
| 148 |
-
Only the `const` and `volatile` type-specifiers at the outermost level
|
| 149 |
-
of the parameter type specification are ignored in this fashion;
|
| 150 |
-
`const` and `volatile` type-specifiers buried within a parameter type
|
| 151 |
-
specification are significant and can be used to distinguish
|
| 152 |
-
overloaded function declarations.[^1] In particular, for any type `T`,
|
| 153 |
-
“pointer to `T`”, “pointer to `const` `T`”, and “pointer to `volatile`
|
| 154 |
-
`T`” are considered distinct parameter types, as are “reference to
|
| 155 |
-
`T`”, “reference to `const` `T`”, and “reference to `volatile` `T`”.
|
| 156 |
-
- Two parameter declarations that differ only in their default arguments
|
| 157 |
-
are equivalent.
|
| 158 |
-
\[*Example 8*:
|
| 159 |
-
Consider the following:
|
| 160 |
-
``` cpp
|
| 161 |
-
void f (int i, int j);
|
| 162 |
-
void f (int i, int j = 99); // OK: redeclaration of f(int, int)
|
| 163 |
-
void f (int i = 88, int j); // OK: redeclaration of f(int, int)
|
| 164 |
-
void f (); // OK: overloaded declaration of f
|
| 165 |
-
|
| 166 |
-
void prog () {
|
| 167 |
-
f (1, 2); // OK: call f(int, int)
|
| 168 |
-
f (1); // OK: call f(int, int)
|
| 169 |
-
f (); // error: f(int, int) or f()?
|
| 170 |
-
}
|
| 171 |
-
```
|
| 172 |
-
|
| 173 |
-
— *end example*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