【发布时间】:2019-07-20 04:15:49
【问题描述】:
我正在尝试为按位
我希望 static_assert 会告诉编译器 "file
“cout
#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;
}
【问题讨论】:
-
你希望看到
FRI << 2的结果是什么?
标签: c++ templates overloading operator-keyword