| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
| Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
| ||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean -Integer -Floating-point | ||||||||||||||||
| Character -String -nullptr(C++11) | ||||||||||||||||
| User-defined(C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes(C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
| Type alias declaration(C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
| Special member functions | ||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
| Declarations | ||||
| Function declaration | ||||
| Function parameter list | ||||
| Function definition | ||||
| Function contract specifiers(C++26) | ||||
| Default arguments | ||||
| Variadic arguments | ||||
inline specifier | ||||
| Lambda expressions(C++11) | ||||
| Coroutines(C++20) | ||||
| Replacement functions | ||||
| Function calls | ||||
| Argument-Dependent Lookup (ADL) | ||||
| Function-call operator | ||||
| Function objects | ||||
| Overloading | ||||
| Overload resolution | ||||
| Operator overloading | ||||
| Address of an overload set |
Besidesfunction-call expressions, whereoverload resolution takes place, the name of an overloaded function may appear in the following 7 contexts:
| # | Context | Target |
|---|---|---|
| 1 | initializer in adeclaration of an object orreference | the object or reference being initialized |
| 2 | on the right-hand-side of an built-in assignment expression | the left-hand side of the built-in assignment |
| 3 | as a function call argument | the function parameter |
| 4 | as a user-defined operator argument | the operator parameter |
| 5 | thereturn statement | the return value of a function or conversion |
| 6 | explicit cast orstatic_cast argument | the corresponding cast |
| 7 | constanttemplate argument | the corresponding template parameter |
In each context, the name of an overloaded function may be preceded by address-of operator& and may be enclosed in a redundant set of parentheses.
If the target type contains aplaceholder type, placeholder type deduction is performed, and the following description uses the deduced type as target type. | (since C++26) |
Contents |
When the address of an overloaded function is taken, a setS of functions is selected from the overload set referred to by the name of the overload function:
F is selected for the function typeFT of the target type ifF (after possibly applying thefunction pointer conversion)(since C++17) is identical toFT.[1]S.If the target is of function pointer type or reference to function type,S can only include non-member functions, explicit object member functions(since C++23) and static member functions. If the target is of pointer-to-member-function type,S can only include implicit object member functions.
After forming the setS, functions are elimiated in the following order:
| (since C++20) |
S remains, all function template specializations inS are eliminated ifS also contains a non-template function.
| (since C++20) |
S contains a second function template specialization whose function template ismore specialized than the function template ofspec.After such eliminations (if any), exactly one selected function should remain inS. Otherwise, the program is ill-formed.
int f(int){return1;}int f(double){return2;} void g(int(&f1)(int),int(*f2)(double)){ f1(0); f2(0.0);} template<int(*F)(int)>struct Templ{}; struct Foo{int mf(int){return3;}int mf(double){return4;}}; struct Emp{void operator<<(int(*)(double)){}}; int main(){// 1. initializationint(*pf)(double)= f;// selects int f(double)int(&rf)(int)= f;// selects int f(int)int(Foo::*mpf)(int)=&Foo::mf;// selects int mf(int) // 2. assignment pf= nullptr; pf=&f;// selects int f(double) // 3. function argument g(f, f);// selects int f(int) for the 1st argument// and int f(double) for the second // 4. user-defined operator Emp{}<< f;//selects int f(double) // 5. return valueauto foo=[]()->int(*)(int){return f;// selects int f(int)}; // 6. castauto p=static_cast<int(*)(int)>(f);// selects int f(int) // 7. template argument Templ<f> t;// selects int f(int) // prevent “unused variable” warnings as if by [[maybe_unused]][](...){}(pf, rf, mpf, foo, p, t);}
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| CWG 202 | C++98 | constant template argument was not a context of taking the address of an overloaded function | it is |
| CWG 250 | C++98 | function template specializations generated with non-deduced template arguments were not selected from the overload set | also selected |
| CWG 1153 | C++98 | it was unclear whether a given function type matches the target type | made clear |
| CWG 1563 | C++11 | it was unclear whether list-initialization is a context of taking the address of an overloaded function | made clear |