C++ 模板更喜欢子类函数,而不是更专门的继承函数

C++ 模板更喜欢子类函数,而不是更专门的继承函数,c++,C++,当我使用这样的代码时,我似乎得到了一个意想不到的结果: class Base { public: template <typename T> bool operator <<(int(*fx)(T)) { return true; } }; class Sub : public Base { public: template <typename T> bool operator <<(T t)

当我使用这样的代码时,我似乎得到了一个意想不到的结果:

class Base
{
public:
    template <typename T>
    bool operator <<(int(*fx)(T)) {
        return true;
    }
};
class Sub : public Base
{
public:
    template <typename T>
    bool operator <<(T t) {
        return false;
    }
};

int foo(int a) {
    return a;
}

int main() {
    bool test = Sub() << foo;
    std::cout << "Used " << (test ? "Base" : "Sub") << " class function";
}
类基
{
公众:
模板
bool操作符
Sub::operator您需要通过添加

using Base::operator<<; 

对于该示例,您看到的行为是正确的


<>当C++遇到二进制运算符表达式<代码>谢谢时间解释。
class Sub : public Base
{
public:
    using Base::operator <<;

    template <typename T>
    bool operator <<(T t) {
        return false;
    }
};
using Base::operator<<; 
#include <iostream>
class Base
{
public:
    template <typename T>
    bool operator <<(int(*fx)(T)) {
        return true;
    }
};
class Sub : public Base
{
public:

    using Base::operator<<;  // <--- ADD THIS HERE


    template <typename T>
    bool operator <<(T t) {
        return false;
    }
};

int foo(int a) {
    return a;
}

int main() {
    bool test = Sub() << foo;
    std::cout << "Used " << (test ? "Base" : "Sub") << " class function";
}
class Sub : public Base
{
public:
    template <typename T>
    bool operator <<(T t) {
        return false;
    }
    using Base::operator<<; // NOTE
};