How to convert tuple to byte array in c++11

前端 未结 2 1425
梦毁少年i
梦毁少年i 2021-01-14 16:02

I need to write a function to convert tuple to byte array. The type of tuple may include int, long, double, std::string, char*,etc. The size and type of tuple a

2条回答
  •  被撕碎了的回忆
    2021-01-14 16:54

    You can use Boost Serialization for this task together with a small extension for std::tuple. However, it doesn't turn it into a byte array by default, but into something else. There is also binary_oarchive. Perhaps this fits your needs.

    #include 
    #include 
    
    #include 
    
    #include "serialize_tuple.h" // https://github.com/Sydius/serialize-tuple
    
    int main()
    {
      auto t = std::make_tuple(42,3.14,'a');
    
      std::ofstream ofs("test.dat");
      boost::archive::text_oarchive oa(ofs);
      oa << t;
    }
    

提交回复
热议问题