C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relocation in C++

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 16 May 2022 09:33:07 +0200
Hi Edward, I currently want to understand/point out, what the proposal would do in practice.   You say the identifier is removed from lexical scope that is from the current block in the compilation unit below the [std::]reloc[ate] instruction?   Without reloc it is simple: Within the block automatic variables are valid, they get destroyed, when leaving the block by return or exception.   So no distinguishment between lifetime and lexical scope was needed.   Now you say reloc only refers to the position in the source file, not the lifetime, do you?   With a linear program flow, that is not an issue.   But with a nonlinear program flow (including simple loops or conditionals), this can get really problematic.   void f() {     string s1, s2, s3;     if (flag)         s2 = reloc s1; // removes s1 from lexical scope     else         s3 = reloc s1; // !!! compilation error }   void f2() {     string s1, s2;     if (flag)         s2 = reloc s1; // removes s1 from lexical scope } // !!! destructor of s1 not called, even if !flag     Best, Sebastian   -----Ursprüngliche Nachricht----- Von:Edward Catmur <ecatmur_at_[hidden]> Gesendet:Mo 16.05.2022 08:54 Betreff:Re: [std-proposals] Relocation in C++ An:std-proposals <std-proposals_at_[hidden]>; CC:Sebastian Wittmeier <wittmeier_at_[hidden]>;   On Sat, 14 May 2022 at 21:37, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote: How does the bookkeeping work for automatic variables?   {     std::string src1 = "hello world";     for (int i = 0; i < 10; i++)         if (randint(0, 40) == 0) {             std::string src2 = std::relocate(src1);             break;         }     // what if we use it here?     // std::cout << src1; }   Is a flag or bool saved together with each automatic variable, which could be relocated?   The compiler would not be able to catch each usage after relocate in compile time.   However this is solved, a std::relocate call could be used without a new object. How does this work with the operator reloc syntax?  I'm not sure which proposal or proposals you are referring to. If you want dynamic lifetime tracking you can just use std::optional. The strength of operator reloc is that it removes the identifier from lexical scope; it is not a dynamic operation per se. 

Received on 2022-05-16 07:33:09