fork download
  1. #include <iostream>
  2. #include <tuple>
  3.  
  4. // テンプレート引数が無いときのダミー関数
  5. std::tuple<> GetDeserializedValue(const std::string& data) {return std::tuple<>();}
  6.  
  7. // 変数を一つずつ復元する関数
  8. template<class First, class... Rest>
  9. std::tuple<First, Rest...> GetDeserializedValue(const std::string& data)
  10. {
  11. std::string buffer(data);
  12. const First& value = *reinterpret_cast<const First*>(buffer.data());
  13. buffer.erase(sizeof(First));
  14.  
  15. // 後続するテンプレート引数を使って再帰呼び出しをして、結果のタプルを連結する
  16. return std::tuple_cat(std::tuple<First>(value), GetDeserializedValue<Rest...>(buffer));
  17. }
  18.  
  19. // バッファから変数を復元し、タプルで返す関数
  20. template<class... Types>
  21. std::tuple<Types...> Deserialize(const std::string& data)
  22. {
  23. return GetDeserializedValue<Types...>(data);
  24. }
  25.  
  26. int main() {
  27. Deserialize<int, double>("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  28. return 0;
  29. }
Compilation error #stdin compilation error #stdout 0s 2824KB
stdin
compilation info
prog.cpp: In function 'std::tuple<_Head, _Tail ...> GetDeserializedValue(const std::string&) [with First = double, Rest = {}, std::string = std::basic_string<char>]':
prog.cpp:16:90:   instantiated from 'std::tuple<_Head, _Tail ...> GetDeserializedValue(const std::string&) [with First = int, Rest = {double}, std::string = std::basic_string<char>]'
prog.cpp:23:47:   instantiated from 'std::tuple<_Elements ...> Deserialize(const std::string&) [with Types = {int, double}, std::string = std::basic_string<char>]'
prog.cpp:27:66:   instantiated from here
prog.cpp:16:90: error: no matching function for call to 'GetDeserializedValue(std::string&)'
stdout
Standard output is empty