C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Pseudo const answer

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 20 Mar 2025 12:46:39 +0100
You want a structure with symmetrical function calls around an instruction or a few lines of code or a block. In C++ normally RAII is used to get two related function calls:  - The function for leaving is inserted implicitly by the compiler  - The compiler ensures that the second function is called regardless, how the block is left: normally, early return, goto, break, continue, exceptions  - The related functions are encapsulated within a class and additional data can be specified, which is stored in member variables   Now you want to introduce a separate facility providing a similar structure. Your use case is more specific (pseudo-const). But the structure is the same as RAII. One can implement pseudo-const with RAII methodologies (with support of the container).   -----Ursprüngliche Nachricht----- Von:Jerome Saint-Martin via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 19.03.2025 23:29 Betreff:Re: [std-proposals] Pseudo const answer An:std-proposals_at_[hidden]; CC:Jerome Saint-Martin <jerome.saint-martin_at_[hidden]>; Few answers... Compiler ======= No "goto" between "direct call" and "reverse call" ("goto" is a souvenir from my first computer, a Commodore 64 : I believed no good coder used it, am I wrong ?). The compiler has to refuse such functions to be pseudo const. The exceptions are runtime, pseudo const is for compilation. So compiler ignores them, like it does ignore them in a const function. For a start, when compiling a ppseudi const function, compiler just checks a call to the "reverse call" exists in the same block after the "direct call", with the same parameters. Compiler optimizations, like automatic reverse insertion : later. When a compiler checks a function is const it checks all call are const. Same with pseudo const : all call inside a pseudo const function have to be pseudo const or const. Lifo for embedded pseudo const nested function sounds good to me, even if I have not think about the problem yet   Philisophy : ========= the object is the same at the beginning and at the end of the pseudo const member function   Use case : ========= Do you really want me to detail with C++ sources and durations in the example I already gave you, about determining the top 10 best students at an exam of 10000 results with pseudo const vs sorting all the results and keep the 10 best ?   -------------------------------- De : Std-Proposals <std-proposals-bounces_at_[hidden]> de la part de Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> Envoyé : lundi 17 mars 2025 12:00 À : std-proposals_at_[hidden] <std-proposals_at_[hidden]> Cc : Sebastian Wittmeier <wittmeier_at_[hidden]> Objet : Re: [std-proposals] Pseudo const answer   Okay, let's separate:   Compiler ======== The compiler checks that a function and its declared reverse are called with the same parameters  - what about early return, exceptions, goto, ... Is the compiler strict?  - Do the reverse functions have to be called manually or does the compiler insert them automatically  - Is it possible to nest several of the same functions or different function/reverse function (LIFO principle)?   Design Philosophy ===============  - You call it pseudo-const; do you expect the object to be the same afterwards again?  - Typically data has to be stored to restore the object; insert/erase is easier, erase/insert more difficult; not always the parameters are enough   Use Cases =========  - Can you give use cases? Does it help programming, safety, performance?  - How does it relate to RAII?  - How do the use cases with pseudo-const compare to the same being implemented with RAII solutions?     -----Ursprüngliche Nachricht----- Von: Jerome Saint-Martin via Std-Proposals <std-proposals_at_[hidden]> Gesendet: Mo 17.03.2025 12:49 Betreff: Re: [std-proposals] Pseudo const answer An: std-proposals_at_[hidden]; CC: Jerome Saint-Martin <jerome.saint-martin_at_[hidden]>; The erase/insert was an example (it could have been add and sub, in an Int class). When the compiler compiles a pseudo const function and finds a call to a function which was declared reverse; it just checks the reverse function is called with the same parameters before the end of the pseudo const function. Just the parameters names matters, not what's inside; the compiler just has to store the name of the parameters.   -------------------------------- De : Std-Proposals <std-proposals-bounces_at_[hidden]> de la part de Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> Envoyé : lundi 17 mars 2025 11:24 À : std-proposals_at_[hidden] <std-proposals_at_[hidden]> Cc : Sebastian Wittmeier <wittmeier_at_[hidden]> Objet : Re: [std-proposals] Pseudo const answer   So your erase function erases as many ints as given as parameter? And the insert function inserts them again? Who does store the values in between? Or can/should other values be inserted insted?   So pseudo const only keeps the size of the container, but not the contents the same? What usage does that have? Do the iterators stay valid? There are containers, e.g. sorted trees, where new elements are possibly inserted at other positions within the container.   -----Ursprüngliche Nachricht----- Von: Jerome Saint-Martin via Std-Proposals <std-proposals_at_[hidden]> Gesendet: Mo 17.03.2025 12:26 Betreff: Re: [std-proposals] Pseudo const answer An: std-proposals_at_[hidden]; CC: Jerome Saint-Martin <jerome.saint-martin_at_[hidden]>; The function in the pair of reverse function shall be declared with qualifier "reverse", like : void erase (int) reverse insert (int). So the compiler knows wich function is the reverse. It then just has to check the reverse functions is called after with the same parameters   -------------------------------- De : Std-Proposals <std-proposals-bounces_at_[hidden]> de la part de Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> Envoyé : lundi 17 mars 2025 10:09 À : std-proposals_at_[hidden] <std-proposals_at_[hidden]> Cc : Sebastian Wittmeier <wittmeier_at_[hidden]> Objet : Re: [std-proposals] Pseudo const answer   And how does the compiler remember, which objects were erased? It copies them out? Or the container keeps them, but marks, which where erased?   In both cases, additional space is allocated   -----Ursprüngliche Nachricht----- Von: Jerome Saint-Martin via Std-Proposals <std-proposals_at_[hidden]> Gesendet: Mo 17.03.2025 10:49 Betreff: Re: [std-proposals] Pseudo const answer An: std-proposals_at_[hidden]; CC: Jerome Saint-Martin <jerome.saint-martin_at_[hidden]>; Pseudo const works directly on the container. Compiler will just have to check each time a non const function, like erase, is called in a pseudo const member function, the reverse function exists, and is called (insert if the function called was erase).     -------------------------------- De : Std-Proposals <std-proposals-bounces_at_[hidden]> de la part de Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> Envoyé : samedi 15 mars 2025 18:28 À : std-proposals_at_[hidden] <std-proposals_at_[hidden]> Cc : Sebastian Wittmeier <wittmeier_at_[hidden]> Objet : Re: [std-proposals] Pseudo const answer   How does pseudo-const manages it without using memory for lists of elements, bools or for a copy of the container?   [PS Sorry, answer first went to the wrong message.]   -----Ursprüngliche Nachricht----- Von: Jerome Saint-Martin <jerome.saint-martin_at_[hidden]> Gesendet: Sa 15.03.2025 09:24 Betreff: RE: [std-proposals] Pseudo const answer An: std-proposals_at_[hidden]; CC: Sebastian Wittmeier <wittmeier_at_[hidden]>; Are you sure "introduce a view, adding a boolean (still present) a list (tree) of pseudo removed objects" will it be as fast and clear as pseudo const ? what about memory usage ?   -------------------------------- De : Std-Proposals <std-proposals-bounces_at_[hidden]> de la part de Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> Envoyé : jeudi 13 mars 2025 14:14 À : std-proposals_at_[hidden] <std-proposals_at_[hidden]> Cc : Sebastian Wittmeier <wittmeier_at_[hidden]> Objet : Re: [std-proposals] Pseudo const answer   You could introduce a view that stores either a boolean (still present) or a list (tree) of pseudo-removed objects.   This specific view provides the option for pseudo-removal, searching the top, etc.   As long as the underlying container is not changed, the view should be able to stably perform all those operations.   -----Ursprüngliche Nachricht----- Von: Jerome Saint-Martin via Std-Proposals <std-proposals_at_[hidden]> Gesendet: Do 13.03.2025 14:32 Betreff: [std-proposals] Pseudo const answer An: std-proposals_at_[hidden]; CC: Jerome Saint-Martin <jerome.saint-martin_at_[hidden]>; Hello, I wanted to answer a question about my proposal for pseudoconstness  , posted in february 2018, with jsaintmartin356_at_[hidden], to  : gasper..._at_[hidden] but the site changed. So I created a new account with this mail adress. The question was :  " ... I'm misunderstanding something and you want to provide both a const  and a pseudoconst method on the same class, in which case, please elucidate why that would ever be useful, because I truly cannot think of an example. "   and answer : " Pseudo Const can be useful in this example : " Your are a university director and want to know the best ten students (the "top").  To do this you may sort all the results, and keep the ten best, but this causes tons of useless comparisons. The solution for me is a pseudo const function member :a loop of 10 iterations : "find the best result, remove if from the results", and reinsert the top ten student in the container. As student result shall not be erased, the pseudo constness of the member function guaranties no result were erased " -- Std-Proposals mailing list 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 -- Std-Proposals mailing list 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 -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-20 11:52:01