c++ - 扩展枚举类型

标签 c++ enums

<分区>

Possible Duplicate:
Extending enums in C++?

我正在使用一个将自己的错误集定义为枚举类型的库。

enum error_type {...}

该库还有一个函数,它采用该枚举类型并打印错误。

void set_status(error_type new_error)

如果我想定义自己的错误,并将其提供给 set_status 函数,是否可以以某种方式扩展 error_type 枚举,或者覆盖它?

最佳答案

可能会有一些尖锐的边缘,但我认为这应该适合你:

#include<iostream>

// Helper struct

template<typename T, int N, typename... LIST>
struct whereInType {
  static const int index = -1;
};

template<typename T, int N, typename HEAD, typename... TAIL>
struct whereInType<T,N,HEAD,TAIL...> {
  static const int index = whereInType<T, N+1, TAIL...>::index;
};

template<typename T, int N, typename... TAIL>
struct whereInType<T,N,T,TAIL...> {
  static const int index = N;
};

// The actual union type

template<typename... ENUMS>
class enum_union {
public:
  template<typename ENUM>
  constexpr enum_union(ENUM val) :
    which_enum(whereInType<ENUM,0,ENUMS...>::index),
    value(val) {}
  constexpr operator long long(){
    return static_cast<long long>(which_enum)<<32 | value;
  }
  template<typename ENUM, int IGNORE=0>
  constexpr bool operator==(const ENUM& e) {
    return *this == enum_union<ENUMS...>(e);
  }
  template<int IGNORE=0>
  constexpr bool operator==(const enum_union<ENUMS...>& oth) {
    return which_enum==oth.which_enum && value==oth.value;
  }
  template<typename T>
  constexpr bool operator!=(const T& oth) {
    return !(*this == oth);
  }
private:
  int which_enum;
  int value;
};

// An example usage

enum normal_errors {
  E_OUTOFMEMORY,
  E_IOERROR
};

enum weird_errors {
  E_OUTOFAARDVARKS,
  E_DIVIDEBYCUCUMBER
};

typedef enum_union<normal_errors, weird_errors> any_error;

// Some tests

void print(any_error e) {
  switch(e) {
  case any_error(E_OUTOFMEMORY):
    std::cout << "Out of Memory\n";
    break;
  case any_error(E_IOERROR):
    std::cout << "I/O Error\n";
    break;
  case any_error(E_OUTOFAARDVARKS):
    std::cout << "WE NEED AARDVARKS!!!  NOW!!!!!\n";
    break;
  case any_error(E_DIVIDEBYCUCUMBER):
    std::cout << "please reinstall universe\n";
    break;
  }
}  

main(){
  print(E_OUTOFMEMORY);
  print(E_IOERROR);
  print(E_OUTOFAARDVARKS);
  print(E_DIVIDEBYCUCUMBER);
  if (any_error(E_OUTOFMEMORY) == E_OUTOFAARDVARKS) {
    std::cout<<"bad\n";
  }else{
    std::cout<<"good\n";
  }
  if (any_error(E_OUTOFMEMORY) != E_OUTOFAARDVARKS) {
    std::cout<<"good\n";
  }else{
    std::cout<<"bad\n";
  }
  if (any_error(E_OUTOFMEMORY) == E_OUTOFMEMORY) {
    std::cout<<"good\n";
  }else{
    std::cout<<"bad\n";
  }
  if (any_error(E_OUTOFMEMORY) != E_OUTOFMEMORY) {
    std::cout<<"bad\n";
  }else{
    std::cout<<"good\n";
  }
}

关于c++ - 扩展枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14503620/

相关文章:

c++ - 对不同的参数使用工厂方法模式

c++ - C++中的命名空间级别枚举

c++ - 在opencv中找到相位相关矩阵中的所有峰

c++ - C++ 枚举中的枚举

c# - C#枚举默认实现了哪些接口(interface)

c++ - 错误 c2440 '=' 无法从 int * 转换为 Type<T> *

c++ - 如何从 C++ 中的格式化字符串派生整数?

java - 运行时类型识别

c++ - 已签名基础类型枚举的位域溢出

java - Java 枚举的奇怪行为