fork(1) download
  1. #include <iostream>
  2.  
  3. template<typename type>
  4. struct tmp
  5. {
  6. tmp() {}
  7. tmp(const tmp &) = default;
  8. tmp(tmp &&) = default;
  9. tmp(const tmp &&o):tmp(o) {}
  10. tmp(tmp &o):tmp(const_cast<tmp const&>(o)){}
  11.  
  12. template<class... Ts>
  13. tmp(Ts&&...ts) : v{std::forward<Ts>(ts)...} {}
  14.  
  15. ~tmp () { std::cout << "tmp destructor\n"; }
  16.  
  17. type v;
  18. };
  19.  
  20. struct noisy {
  21. ~noisy() { std::cout << "bang\n"; }
  22. };
  23.  
  24. template<class X, class... Ts>
  25. tmp<X> wrap_as_tmp(Ts&&... ts) // used 'tmp<int [4]>' instead of array to track object destruction (but normally it should be an native array type
  26. {
  27. return {std::forward<Ts>(ts)...};
  28. }
  29.  
  30. int main() {
  31. auto&& x = wrap_as_tmp<noisy[4]>().v;
  32. std::cout << "There\n";
  33. }
  34.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
There
tmp destructor
bang
bang
bang
bang