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

后端 未结 1 456
既然无缘
既然无缘 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-16 16:10

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

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

    etc.

    And use them in make

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

    When getting the value, use

    template 
    T get(std::string_view name)
    {
       // Read the type name and make sure it is equal to persistent_type::name().
    
       // Rest of your logic to read the object
    }
    

    0 讨论(0)
提交回复
热议问题