Déménagement - le siècle dernier! Alternatives à std :: move dans «C ++ of the Future»

, , , , , , move- move-. , std::mutex, : , , — , «».


, ( Rust !) move- . , move- C++ : move- , . , , .


— , . , move-. : open_file ( file), not_null_unique_ptr<T> ( unique_ptr<T>).


Arthur O'Dwyer, , « ». , !


C++, : . , : ?


  1. move- ,
  2. . . , .
  3. move- , - ,
  4. , , «» , . , : ,

, .


P1144: Trivially relocatable


, Arthur O'Dwyer, [[trivially_relocatable]], , , move. , memcpy , . , , , , .


. : [[trivially_relocatable]], , move- (rule of zero). [[trivially_relocatable]] , , . std::vector relocate_at, relocation move, , .


template <typename T>
class [[trivially_relocatable]] unique_ptr { ... };

std::vector<unique_ptr<widget>> v;
for (auto x : ...) {
  //  unique_ptr   relocation,   move
  v.push_back(std::make_unique<widget>(x));
}

proposal , :


  • [[trivially_relocatable]], . , std::mutex, [[trivially_relocatable]]
  • ( )
  • Trivially relocatable . , std::unique_ptr<T> -

P2025: Guaranteed NRVO


proposal , , , . , «». , P2025 .


C++17 , return . Return Value Optimization (RVO). P2025 , (NRVO). -, std::mutex :


widget setup_widget(int x) {
  return widget(x);  // OK, C++17
}

widget setup_widget(int x) {
  auto w = widget(x);
  w.set_y(process(x));
  return w;  // OK, P2025
}

, proposal :)


P0927: Lazy parameters


, @autoclosure Swift. , . , , :


void vector<T>::super_emplace_back([] -> T value) {
  void* p = reserve_memory();
  new (p) T(value());
}

vector<widget> v;
v.super_emplace_back(widget());  //  move
v.super_emplace_back([&] { return widget(); });  //  

P0573: Abbreviated lambdas


, , . - « » C++ , . , P0573 , , , :


//  
auto add = [&](auto&& x, auto&& y) { return x + y; };
auto dbl = [&](auto&& x) { return x * 2; };
auto life = [&] { return 42; };

// P0573
auto add = [&](x, y) => x + y;
auto dbl = [&](x) => x * 2;
auto life = [&]() => 42;

//  #1:  Rust
auto add = |x, y| x + y;
auto dbl = |x| x * 2;
auto life = || 42;

//  #2
auto add = x y: x + y;
auto dbl = x: x * 2;
auto life = :42;

! C++23. , , .

Source: https://habr.com/ru/post/fr484380/


All Articles