Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Enums_Enum Class - Fatal编程技术网

C++ 枚举基类型的模板优化

C++ 枚举基类型的模板优化,c++,templates,enums,enum-class,C++,Templates,Enums,Enum Class,下面有两个代码示例。它们都将枚举或枚举类解释为其基础类型。当使用多个不同的枚举时,编译后哪个更小 处理数据序列化项目时,我需要将枚举强制转换为其基础类型,即各种大小的有符号和无符号整数。我认为有两种方法可以实现这一点 在第一种情况下,枚举作为模板参数传递: template<class ENUM> class my_enum { private: // Check if the given template parameter is indeed an enum or e

下面有两个代码示例。它们都将枚举或枚举类解释为其基础类型。当使用多个不同的枚举时,编译后哪个更小

处理数据序列化项目时,我需要将枚举强制转换为其基础类型,即各种大小的有符号和无符号整数。我认为有两种方法可以实现这一点

在第一种情况下,枚举作为模板参数传递:

template<class ENUM>
class my_enum
{
  private:
    // Check if the given template parameter is indeed an enum or enum class.
    static_assert(std::is_enum<ENUM>::value, "This class only supports enum and enum classes as template parameter.");

    // Get the underlying type of the enum.
    typedef INT_TYPE = std::underlying_type<ENUM>::type;

  public:
    // Useful code

  private:
    // The actual data
    INT_TYPE data; 
};
template<class INT_TYPE>
class my_enum2
{
  public:
    // Useful code

  private:
    // The actual data
    INT_TYPE data; 
};
模板
类我的枚举
{
私人:
//检查给定的模板参数是否确实是枚举或枚举类。
static_assert(std::is_enum::value,“该类仅支持enum和enum类作为模板参数”);
//获取枚举的基础类型。
typedef INT_TYPE=std::底层_TYPE::TYPE;
公众:
//有用代码
私人:
//实际数据
INT_型数据;
};
使用此选项看起来像:

enum enumA {
 x = 0,
 y,
 z
}

enum enumB {
 a = -10,
 b = -30,
 c =  100
}

my_enum<enumA> A;
my_enum<enumB> B;
enum enumA{
x=0,
Y
Z
}
枚举{
a=-10,
b=-30,
c=100
}
我的名字是A;
我的名字是B;
我看到的第二种可能性是直接将基础类型作为模板参数传递:

template<class ENUM>
class my_enum
{
  private:
    // Check if the given template parameter is indeed an enum or enum class.
    static_assert(std::is_enum<ENUM>::value, "This class only supports enum and enum classes as template parameter.");

    // Get the underlying type of the enum.
    typedef INT_TYPE = std::underlying_type<ENUM>::type;

  public:
    // Useful code

  private:
    // The actual data
    INT_TYPE data; 
};
template<class INT_TYPE>
class my_enum2
{
  public:
    // Useful code

  private:
    // The actual data
    INT_TYPE data; 
};
模板
类我的_enum2
{
公众:
//有用代码
私人:
//实际数据
INT_型数据;
};
这将被用作:

my_enum2<std::underlying_type<enumA>::type> A;
my_enum2<std::underlying_type<enumB>::type> B;
my_enum2 A;
我的2 B;
我看到最后一个选项只为不同大小的单整数和无符号整数生成4-6个实现。然而,写出定义并不是那么简单

第一个类会为每个枚举类型或每个基础类型生成实例化吗?

因为
my_enum
my_enum
是不同的类型,所以它们会得到单独的实例化,即使生成的代码是相同的


第二个版本将枚举的基础类型作为模板参数传递,这将减少代码量,因为
enumA
enumB
都将使用与模板参数相同的类型,生成相同的模板类型。

您可以使用模板别名,以便使用方法2实现更清晰的语法<代码>使用my_enum_别名的模板=my_enum2@Jarod42那太好了,除了用调试器一步一步地检查外,还有其他方法可以找到这个问题吗?