fork download
  1. #include <iostream>
  2. #include <iostream>
  3.  
  4. template<class... T>
  5. struct non_copyable_type {
  6. non_copyable_type() = delete;
  7. non_copyable_type(const non_copyable_type&) = delete;
  8. non_copyable_type(const T&...) {};
  9. };
  10.  
  11. using T1 = non_copyable_type<int>;
  12. using T2 = T1;
  13. using TL1 = non_copyable_type<T1, T2>;
  14.  
  15. using foo_t = non_copyable_type<T1, T2, TL1>;
  16.  
  17. template<class... Args>
  18. void side_effects(Args&&... args) {
  19. std::cout << "side effects" << std::endl;
  20. }
  21. template<class... Args>
  22. void second_side_effects(Args&&... args) {
  23. std::cout << "second side effects" << std::endl;
  24. }
  25. template<class... Args>
  26. void final_side_effects(Args&&... args) {
  27. std::cout << "final side effects" << std::endl;
  28. }
  29.  
  30. struct C {
  31. C(const T1& arg_1, const T2& arg_2) {
  32. side_effects(arg_1, arg_2);
  33. TL1 local_1(arg_1, arg_2);
  34. second_side_effects(arg_1, arg_2, local_1);
  35. foo_t f(arg_1, arg_2, local_1); // the actual construction
  36. final_side_effects(arg_1, arg_2, local_1, f);
  37. }
  38. };
  39.  
  40. struct C2 {
  41. C2(const T1& arg_1, const T2& arg_2)
  42. : C2(arg_1, arg_2
  43. ,([](const T1& a, const T2& b){
  44. side_effects(a, b);
  45. }(arg_1, arg_2), TL1(arg_1, arg_2))) {}
  46.  
  47. C2(const T1& arg_1, const T2& arg_2, TL1&& local_1)
  48. : C2(arg_1, arg_2
  49. ,[](const T1& a, const T2& b, TL1& c) -> TL1& {
  50. second_side_effects(a, b, c);
  51. return c;
  52. }(arg_1, arg_2, local_1)) {}
  53.  
  54. C2(const T1& arg_1, const T2& arg_2, TL1& local_1)
  55. : f(arg_1, arg_2, local_1) {
  56. final_side_effects(arg_1, arg_2, local_1, f);
  57. }
  58. private:
  59. foo_t f;
  60. };
  61.  
  62. int main() {
  63. T1 a(0);
  64. T2 b(0);
  65. C c1(a, b);
  66. C2 c2(a, b);
  67. }
Success #stdin #stdout 0s 4480KB
stdin
Standard input is empty
stdout
side effects
second side effects
final side effects
side effects
second side effects
final side effects