Tutorial by Examples

An overload without conversions needed for parameter types or only conversions needed between types that are still considered exact matches is preferred over an overload that requires other conversions in order to call. void f(int x); void f(double x); f(42); // calls f(int) When an argument b...
Overload resolution partitions the cost of passing an argument to a parameter into one of four different categorizes, called "sequences". Each sequence may include zero, one or several conversions Standard conversion sequence void f(int a); f(42); User defined conversion seque...
Overload resolution occurs after name lookup. This means that a better-matching function will not be selected by overload resolution if it loses name lookup: void f(int x); struct S { void f(double x); void g() { f(42); } // calls S::f because global f is not visible here, ...
You must be very careful when providing a forwarding reference overload as it may match too well: struct A { A() = default; // #1 A(A const& ) = default; // #2 template <class T> A(T&& ); // #3 }; The intent here was that A is c...
The steps of overload resolution are: Find candidate functions via name lookup. Unqualified calls will perform both regular unqualified lookup as well as argument-dependent lookup (if applicable). Filter the set of candidate functions to a set of viable functions. A viable function for whi...
Converting an integer type to the corresponding promoted type is better than converting it to some other integer type. void f(int x); void f(short x); signed char c = 42; f(c); // calls f(int); promotion to int is better than conversion to short short s = 42; f(s); // calls f(short); exact mat...
The following examples will use this class hierarchy: struct A { int m; }; struct B : A {}; struct C : B {}; The conversion from derived class type to base class type is preferred to user-defined conversions. This applies when passing by value or by reference, as well as when converting pointe...
Passing a pointer argument to a T* parameter, if possible, is better than passing it to a const T* parameter. struct Base {}; struct Derived : Base {}; void f(Base* pb); void f(const Base* pb); void f(const Derived* pd); void f(bool b); Base b; f(&b); // f(Base*) is better than f(const...

Page 1 of 1