C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Set 'this' in non-member function

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 9 Oct 2023 09:09:35 +0200
For your motivating example for code reuse without renaming of used variables there are lots of other solutions, e.g. using functions or lambdas or a fixed reference variable, which is set specifically before the code block and used throughout, or inheriting from the type of the object and creating member functions.     To use member variables without having to type the name of the object, other programming languages offer topicalizing blocks.  - https://dlang.org/spec/statement.html#WithStatement (D WITH statement)  - https://www.freepascal.org/docs-html/ref/refsu62.html (Pascal WITH statement)  - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement (Basic WITH statement)  - https://docs.raku.org/syntax/given (GIVEN statement in Raku, which is a mixture of WITH and C-like switch)  - https://kotlinlang.org/docs/scope-functions.html#with (Kotlin WITH scope function, implemented in a library)  - https://docs.python.org/3/reference/compound_stmts.html#the-with-statement (Python WITH statement)  - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with (Javascript WITH statement)     I am not aware of a formal C++ proposal. But I found a discussion here:  - https://gcc.gnu.org/legacy-ml/gcc/2003-01/msg00167.html ("Re: c++ "with" keyword" on GCC project mailing list)   and of course StackOverflow  - https://stackoverflow.com/questions/2279180/does-c-have-with-keyword-like-pascal (Does C++ have "with" keyword like Pascal?)  - https://stackoverflow.com/questions/4054946/is-this-the-best-way-to-do-a-with-statement-in-c (Is this the best way to do a "with" statement in C++?)  - https://stackoverflow.com/questions/11491505/python-with-statement-in-c (Python with statement in C++)     .           -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 09.10.2023 08:16 Betreff:[std-proposals] Set ‘this‘ in non-member function An:std-proposals <std-proposals_at_[hidden]>; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; It would be cool if we could do the following: class Monkey {    int i; }; void Func(Monkey &arg) {    Monkey *const this = &arg;    i = 6;      // Implicitly tries 'this->i' } int main(void) {    Monkey obj;    Func(obj); } To give an example, when I'm writing a desktop GUI program in wxWidgets, I sometimes have to write a standalone function outside the class, and then I copy-paste a load of code from the class implementation into my own standalone function -- but it doesn't compile because I need to write "this->' in front of all the accesses of member variables/functions. -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals  

Received on 2023-10-09 07:09:37