C++ Logo


Advanced search

[std-proposals] Relocation in C++

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Sat, 14 May 2022 22:37:15 +0200
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?   int cube(int base) {     int square = base * base;     int result = square * base;     // we do not need square anymore     std::relocate(square); // this should end the scope for temporary variables early     return result; }   This would be equivalent to   int cube(int base) {     int result;     {         int square = base * base;         result = square * base;     } // we do not need square anymore, end scope for temporary variable       return result; }   Best, Sebastian   -- Sebastian Wittmeier

Received on 2022-05-14 20:37:18