C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Simplify taking pointer to non-static member function

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 6 Mar 2024 22:10:20 +0100
The pointer to member function is called `&C::f` instead of `&f` to make it corresponding to pointers to member variables. It is possible to create a direct pointer to a member variable, but it is not possible to create a direct pointer to a member function.   I also do not agree with the "I don't care" ones: They should stay forbidden: The decay of the functions to pointers to functions is a C anachronism: We should either use &f or f(), but never f, except for calling.   As a _theoretical_ (!) alternative, references to member variables and references to member functions could be introduced (but are they really needed? And it would not work the same way with free functions due to C compatibility. And references to member variables would not work as the syntax is already used as alternative for normal references, see `d` ):   void freef() { }   class C {      int f(char);      int a = 4;      static void g() {   // pointer to member functions     int (C::*fp3)(char) = &C::f; // current     int (C::*fp4)(char) = &f; // new // pointer to free function     void(*f1)() = &freef;// current     void(*f2)() = freef;// current     void(*f3)() = C::s;// current // references to member functions     int (C::&fp5)(char) = C::f; // new, but similar syntax is already used for static functions one line above     int (C::&fp6)(char) = f; // new, but syntax is already used for three lines above  // pointer to member variable     int C::*c = &C::a; // current  // reference to variable, which is actually member variable     int& d = C::a; // current     int& d2 = a; // current  // reference to member variable     int C::&e = C::a; // new, but C::a is already used two lines above! // pointer to local variable     int* f = &b; // current   // reference to local variable     int& g = b; // current    } }         -----Ursprüngliche Nachricht----- Von:Oleksandr Pikozh via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 06.03.2024 13:17 Betreff:Re: [std-proposals] Simplify taking pointer to non-static member function An:std-proposals_at_[hidden]; CC:Oleksandr Pikozh <o.pikozh_at_[hidden]>; I personally like the idea to allow a shorter way (or more ways) of getting non-static member function address:      class C {       int f(char);       static void g() {         int C::*fp1(char) = &C::f; // allowed         int C::*fp2(char) = &f; // forbidden but IMHO should be allowed         int C::*fp3(char) = C::f; // forbidden and I don't care         int C::*fp4(char) = f; // forbidden and I don't care       }     };  As for the proposed change of the expression type (so that `&C::f` would contain pointer to `this`, i.e. in fact act as `std::bind_front(&C::f, this)` instead) – I don't support that, IMHO that's too incompatible change. On Tue, Mar 5, 2024, 20:02 ஜெய்கணேஷ் குமரன் via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote: Remember: we're talking about querying information that you *already have*. The "this pointer of the current instance" is clearly available to whomever is writing `&class_name::function`. Besides your personal convenience, what's the point here? Besides personal convenience, there is no point, yes. Anyway, do consider much original suggestion of simply not needing to quality non-static member functions with class name, even if you do not consider this. -- Std-Proposals mailing list Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-03-06 21:10:22