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

时间:2015-05-07 19:46:36

标签: c++ templates overloading

我想在类模板中为operator |制作enum的模板重载。

这是一个最小的例子:

#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编译此代码。

此代码有什么问题?

1 个答案:

答案 0 :(得分:2)

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

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

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

DEMO

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

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

另见:

相关问题