在 class 模板的成员枚举上重载运算符

Overloading operator on member enum of class template

我想为 class 模板中的 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
}

Live example.

编译器在此代码中显示以下诊断信息:

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 编译这段代码。

这段代码有什么问题?

问题是在 typename test1<T>::test2 中,模板参数 Tnon-deduced context 中,因此 C++ 无法从函数参数的类型推导出其模板参数。

您可以通过显式实例化函数模板看到这一点:

test1<int>::test2 flags = operator | <int> (test1<int>::test3, test1<int>::test4);

DEMO

或者使用class模板的非模板友元函数:

template <class T>
struct test1 {
    enum test2 {
        test3, test4
    };
    friend test2 operator | (test2 f1, test2 f2) {
        return static_cast<test2>(
            static_cast<unsigned>(f1) | static_cast<unsigned>(f2)
        );
    }
};

int main() {
    test1<int>::test2 flags = test1<int>::test3 | test1<int>::test4;
}

DEMO

另请参阅:

  • g++ compilation error of another thing that works in msvs
  • Candidate template ignored because template argument could not be inferred