shushus

Спринт 14. Variadic Templates. Задание 2.

May 5th, 2023
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. apply_to_many.cpp
  3. -----------------------------------------------------------------------
  4. #include <cassert>
  5. #include <memory>
  6. #include <string>
  7.  
  8. template <typename T, typename... Args>
  9. void ApplyToMany(T& func, Args&&... args) {
  10.     (func(std::forward<Args>(args)), ...);
  11. }
  12.  
  13. void TestSum() {
  14.     int x;
  15.     auto lambda = [&x](int y) {x += y;};
  16.  
  17.     x = 0;
  18.     ApplyToMany(lambda, 1);
  19.     assert(x == 1);
  20.  
  21.     x = 0;
  22.     ApplyToMany(lambda, 1, 2, 3, 4, 5);
  23.     assert(x == 15);
  24. }
  25.  
  26. void TestConcatenate() {
  27.     using namespace std::literals;
  28.     std::string s;
  29.    
  30.     auto lambda = [&s](const auto& t) {
  31.        
  32.         if (!s.empty()) {s += " ";}
  33.         s += t;
  34.     };
  35.  
  36.     ApplyToMany(lambda, "cyan"s, "magenta"s, "yellow"s, "black"s);
  37.     assert(s == "cyan magenta yellow black"s);
  38. }
  39.  
  40. void TestIncrement() {
  41.     auto increment = [](int& x) {++x;};
  42.  
  43.     int a = 0;
  44.     int b = 3;
  45.     int c = 43;
  46.  
  47.     ApplyToMany(increment, a, b, c);
  48.    
  49.     assert(a == 1);
  50.     assert(b == 4);
  51.     assert(c == 44);
  52. }
  53.  
  54. void TestArgumentForwarding() {
  55.    
  56.     struct S {
  57.        
  58.         int call_count = 0;
  59.         int i = 0;
  60.         std::unique_ptr<int> p;
  61.        
  62.         void operator()(int i) {
  63.             this->i = i;
  64.             ++call_count;
  65.         }
  66.        
  67.         void operator()(std::unique_ptr<int>&& p) {
  68.             this->p = std::move(p);
  69.             ++call_count;
  70.         }
  71.     };
  72.  
  73.     S s;
  74.  
  75.     ApplyToMany(s, 1, std::make_unique<int>(42));
  76.    
  77.     assert(s.call_count == 2);
  78.     assert(s.i == 1);
  79.     assert(s.p != nullptr && *s.p == 42);
  80. }
  81.  
  82. void TestArgumentForwardingToConstFunction() {
  83.    
  84.     struct S {
  85.        
  86.         mutable int call_count = 0;
  87.         mutable int i = 0;
  88.         mutable std::unique_ptr<int> p;
  89.        
  90.         void operator()(int i) const {
  91.             this->i = i;
  92.             ++call_count;
  93.         }
  94.        
  95.         void operator()(std::unique_ptr<int>&& p) const {
  96.             this->p = std::move(p);
  97.             ++call_count;
  98.         }
  99.     };
  100.  
  101.     const S s;
  102.    
  103.     ApplyToMany(s, 1, std::make_unique<int>(42));
  104.    
  105.     assert(s.call_count == 2);
  106.     assert(s.i == 1);
  107.     assert(s.p != nullptr && *s.p == 42);
  108. }
  109.  
  110. int main() {
  111.     TestSum();
  112.     TestConcatenate();
  113.     TestIncrement();
  114.     TestArgumentForwarding();
  115.     TestArgumentForwardingToConstFunction();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment