【发布时间】:2015-05-07 19:46:36
【问题描述】:
我想为类模板中的enum 重载operator |。
这是一个最小的例子:
#include <iostream>
using namespace std;
template <class T>
struct test1 {
enum test2 {
test3, test4
};
};
template <class T>
typename test1<T>::test2 operator | (typename test1<T>::test2 f1, typename test1<T>::test2 f2) {
return static_cast<typename test1<T>::test2>(
static_cast<unsigned>(f1) | static_cast<unsigned>(f2)
);
}
int main() {
test1<int>::test2 flags = test1<int>::test3 | test1<int>::test4; // error here
}
编译器在此代码中显示以下诊断:
In function 'int main()':
error: invalid conversion from 'int' to 'test1<int>::test2' [-fpermissive]
test1<int>::test2 flags = test1<int>::test3 | test1<int>::test4;
我还尝试使用 LLVM 6.1.0 编译此代码。
这段代码有什么问题?
【问题讨论】:
标签: c++ templates overloading