Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <array>
  2.  
  3. template <typename T, typename ...Args>
  4. inline std::array<T, sizeof...(Args)> make_array(Args &&...args) {
  5. return std::array<T, sizeof...(Args)>{ std::forward<Args>(args)... };
  6. }
  7.  
  8. #define MAKE_ARRAY(T, ...) decltype(make_array<T>(__VA_ARGS__)){__VA_ARGS__}
  9.  
  10. #include <typeinfo>
  11. #include <iostream>
  12. #include <algorithm>
  13.  
  14. struct object {
  15. object(): value(0) { std::cout << "def-ctor\n"; }
  16. object(int v): value(v) { std::cout << "ctor( " << value << " )\n"; }
  17. object(object &&o) { std::cout << "move-ctor( " << o.value << " )\n"; value = o.value; o.value = -value; }
  18. object(const object &o) { std::cout << "copy-ctor( " << o.value << " )\n"; value = o.value; }
  19. object &operator =(object &&o) { std::cout << "move-assign( " << o.value << " to " << value << " )\n"; value = o.value; o.value = -value; }
  20. object &operator =(const object &o) { std::cout << "copy-assign( " << o.value << " to " << value << " )\n"; value = o.value;}
  21. ~object() { std::cout << "destructor( " << value << " )\n"; }
  22. int value;
  23. };
  24.  
  25. int main() {
  26. auto a = MAKE_ARRAY(object, object(1), object(2), object(3));
  27. std::cout << typeid(a).name() << '\n';
  28. std::for_each(a.begin(), a.end(), [](const object &o) {
  29. std::cout << o.value << ", ";
  30. });
  31. std::cout << '\n';
  32. }
Add Comment
Please, Sign In to add comment