fork(2) download
  1. #include <type_traits>
  2. #include <tuple>
  3.  
  4. template<int... Is>
  5. struct seq { };
  6.  
  7. template<int N, int... Is>
  8. struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
  9.  
  10. template<int... Is>
  11. struct gen_seq<0, Is...> : seq<Is...> { };
  12.  
  13. template<typename... Args, int... Is>
  14. auto make_pointer_tuple(std::tuple<Args...>& t, seq<Is...>)
  15. -> std::tuple<typename std::add_pointer<Args>::type...>
  16. {
  17. return std::forward_as_tuple(&std::get<Is>(t)...);
  18. }
  19.  
  20. template<typename... Args>
  21. auto make_pointer_tuple(std::tuple<Args...>& t)
  22. -> std::tuple<typename std::add_pointer<Args>::type...>
  23. {
  24. return make_pointer_tuple(t, gen_seq<sizeof...(Args)>());
  25. }
  26.  
  27. #include <string>
  28. #include <iostream>
  29.  
  30. int main()
  31. {
  32. std::tuple<int, bool, std::string> myFavoriteTuple{0, false, ""};
  33.  
  34. int* pInt = nullptr;
  35. bool* pBool = nullptr;
  36. std::string* pString = nullptr;
  37. tie(pInt, pBool, pString) = make_pointer_tuple(myFavoriteTuple);
  38.  
  39. *pInt = 42;
  40. *pBool = true;
  41. *pString = "Hello, World!";
  42.  
  43. std::cout << std::get<0>(myFavoriteTuple) << std::endl;
  44. std::cout << std::get<1>(myFavoriteTuple) << std::endl;
  45. std::cout << std::get<2>(myFavoriteTuple) << std::endl;
  46. }
  47.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
42
1
Hello, World!