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

后端 未结 1 455
既然无缘
既然无缘 2021-01-16 15:54

Edit: highlighting the actual question with more context available if desired.

I want to implement the following method:

template &l         


        
相关标签:
1条回答
  • 2021-01-16 16:10

    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
    }
    
    0 讨论(0)
提交回复
热议问题