如何重载枚举的<<运算符

How to overload the << operator for enums

本文关键字:运算符 枚举 何重载 重载      更新时间:2023-10-16

我正在尝试为按位<<运算符编写一个通用模板重载,但我很难指定运算符的左侧是枚举,并且编译器给了我一个"不明确的重载"错误。

我希望static_assert会告诉编译器"file <<x"中的<<运算符不是我定义的模板,然后它会选择iostream中定义的<<运算符。但是,我收到一条编译器消息,说两者是模棱两可的。

它没有"cout <<x"的问题。

  #include <fstream>
  #include <iostream>
  using namespace std;
  enum DAY{MON=1,TUES=2,WED=4,THUR=8,FRI=16};
  template<typename Enum>  
  constexpr Enum operator <<(Enum e,int n)  
  {
    static_assert(std::is_enum<Enum>::value, 
    "template parameter is not an enum type");
    using underlying = typename std::underlying_type<Enum>::type;
    return static_cast<Enum> ( static_cast<underlying>(e) << n );
  }
  int main()
  {
  // this does as I'd like
    DAY day = MON;
    day = static_cast<DAY>(day << 2); // this is the behavior I need
    cout << day << endl;
// but this is ambiguous
    ofstream file("test.dat");
    float x;
    file << x; // this line is ambigous
    return 0;
  }

static_assert应用太晚,无法从过载分辨率中删除operator<<。 您需要改用 SFINAE。 像这样的事情会做你想做的事:

template <typename Enum>
constexpr std::enable_if_t<std::is_enum_v<Enum>, Enum>
operator<<(Enum e, int n)
{
    using underlying = typename std::underlying_type_t<Enum>;
    return static_cast<Enum> ( static_cast<underlying>(e) << n );
}

现场演示