Advertisement
RobertDeMilo

RB5.5 Когда перемещение не помогает

Apr 17th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. Если у объекта много данных на стеке, перемещение не поможет или поможет плохо
  2.  
  3. Вызов move для константного объекта бесполезен. Следите за константностью перемещаемого объекта.
  4.  
  5. //#include <iostream>
  6. //#include <vector>
  7. //#include <array>
  8.  
  9. //using namespace std;
  10.  
  11. //const int SIZE = 10'000;
  12.  
  13. //array<int, SIZE> MakeArray()
  14. //{
  15. //  array<int, SIZE> a;
  16. //  a.fill(8);
  17. //  return a;
  18. //}
  19. //
  20. //int main()
  21. //{
  22. //  {
  23. //      LOG_DURATION("with variable");
  24. //
  25. //      vector<array<int, SIZE>> arrays;
  26. //      for (int i = 0; i < 10000; ++i)
  27. //      {
  28. //          auto heavy_array = MakeArray();
  29. //          arrays.push_back(heavy_array);
  30. //      }
  31. //  }
  32. //  {
  33. //      LOG_DURATION("without variable");
  34. //
  35. //      vector<array<int, SIZE>> arrays;
  36. //      for (int i = 0; i < 10000; ++i)
  37. //      {
  38. //          arrays.push_back(MakeArray());
  39. //      }
  40. //      
  41. //  }
  42. //  
  43. //  return 0;
  44. //}
  45.  
  46. #include <iostream>
  47. #include <string>
  48. #include <vector>
  49. #include <utility>
  50.  
  51. using namespace std;
  52.  
  53. string MakeString()
  54. {
  55.     return "C++";
  56. }
  57.  
  58. int main()
  59. {
  60.     vector<string> strings;
  61.     const string s = MakeString();
  62.     cout << s << "\n";
  63.     strings.push_back(move(s));
  64.     cout << s << "\n";
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement