Persist C++ type info to file for use across program invocations

一曲冷凌霜 提交于 2019-12-01 14:10:01
R Sahu

You can add code to define the persistent names of the types you wish to persist.

template <typename T> struct persistent_type;

template <> struct persistent_type<int>
{
   static std::string_view name() { return "int"; }
}

template <> struct persistent_type<double>
{
   static std::string_view name() { return "double"; }
}

etc.

And use them in make

template <typename T>
void make(std::string_view name, T value)
{
   // Save the type name of the data, persistent_type<T>::name()

   // Save the name of the data, name

   // Save the data, value
}

When getting the value, use

template <typename T>
T get(std::string_view name)
{
   // Read the type name and make sure it is equal to persistent_type<T>::name().

   // Rest of your logic to read the object
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!