Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 谷物:跨枚举共享序列化函数_C++_Serialization_Enums_Rtti_Cereal - Fatal编程技术网

C++ 谷物:跨枚举共享序列化函数

C++ 谷物:跨枚举共享序列化函数,c++,serialization,enums,rtti,cereal,C++,Serialization,Enums,Rtti,Cereal,让我们假设我有这样的东西: enum t_color { BLUE=0,RED,GREEN}; vector<string> TAG_color={"BLUE", "RED", "GREEN"}; enum t_colores { AZUL=0,ROJO,VERDE}; vector<string> TAG_colores={"AZUL", "ROJO", "VERDE"}; 我想在谷类食品中使用一种常见的节俭方法,例如: template <class A

让我们假设我有这样的东西:

enum t_color { BLUE=0,RED,GREEN};
vector<string> TAG_color={"BLUE", "RED", "GREEN"};

enum t_colores { AZUL=0,ROJO,VERDE};
vector<string> TAG_colores={"AZUL", "ROJO", "VERDE"};
我想在谷类食品中使用一种常见的节俭方法,例如:

  template <class Archive,typename T > inline
  std::string save_minimal( Archive const &, typename std::enable_if< std::is_enum<T>::value, T >::type const & t )
  {
    std::string ret="Unknown";
    if ( std::is_same<T,t_color>::value)
    {
      ret=TAG_color[t];
    }
    else if (  std::is_same<T,t_colores>::value)
    {
      ret=TAG_colores[t];
    }
    return ret;
 }

它可以编译,但似乎Grane忽略了模板。它只使用枚举的整数值

是的。。。这是可行的,但不能直接通过谷物:

#define SERIALIZE_ENUM(enumname)                                               \
template <class Archive> inline                                                \
std::string save_minimal( Archive const &, t_##enumname const & t )            \
{                                                                              \
  return std::string(TAG_##enumname[t]);                                       \
}                                                                              \
template <class Archive> inline                                                \
void load_minimal(Archive const&, t_##enumname & t,std::string  const& value ) \
{                                                                              \
  int a=0;                                                                     \
  for (auto const &tag:TAG_##enumname)                                         \
  {                                                                            \
    if ( tag == value )                                                        \
    {                                                                          \
      t = static_cast<t_##enumname>(a);                                        \
      return;                                                                  \
    }                                                                          \
    a++;                                                                       \
  }                                                                            \

namespace cereal { 
  SERIALIZE_ENUM(color)
  SERIALIZE_ENUM(colores)
}