Why should static member functions have an implicit object parameter in accordance with ยง13.3.1 / 4?

In ยง13.3.1 / 4 (N3337) you will find the following:

For static member functions, the implicit object parameter is considered compatible with any object (since, if a function is selected, the object is discarded).

In ยง9.4.1 / 2, this statement:

A static member function does not have this pointer.

Then, what is the purpose of the implicit parameter of an object for a static member function?

+6
source share
2 answers

He used to facilitate understanding of overload.

struct S { void f(int); static void f(double); }; int main() { S::f(1); } 

Here s::f(1); is simply a blunder because f(int) is a better match than f(double) , even if overloading f(int) causes an additional error.

If the rules were any other way, consider what would happen for this:

 template <typename T> struct U : S { void u() { S::f(1); } }; template <typename T> struct V : U<T> { void v() { S::f(1); } }; 

Here U::u acts explicitly and calls a member function. V<T> , however, has a base class, depending on the type, therefore it is not known when determining the time for determining a template from S The S::f solution for overloading f(double) here will be very confusing.

+3
source

The following is a description from the standard, which implies that the implicit parameter of an object is used for overload purposes.

Before the resolution of the overload starts, functions selected by searching by name and subtracting the template argument are combined to form a set of candidate functions (the exact criteria depend on the context in which the overload is resolved).

If any candidate function is a member function (static or non-static), but not a constructor, it is processed as if it had an additional parameter (implicit parameter of the object) that represents the object for which they are called and appears before the first of actual parameters.

Similarly, the object on which the member function is called is added to the argument list as an argument to the object object

For class X member functions, the parameter type of an implicit object depends on the cv qualifications and ref-qualifications of the member function.

For static member functions, it is assumed that the parameter of the implicit object matches any object: its type is not checked and no conversion sequence is performed for it .

0
source

Source: https://habr.com/ru/post/979175/


All Articles