C++用模板和虚函数标记疯狂

Phi*_* T. 3 c++ templates g++ clang++

在重构一个相当大的代码库的过程中,我的编译器想出了一个误解我的好方法.这是我所说的最简单的例子:

#include <iostream>

class Foo {
public:
        virtual int get() = 0;
        template <typename T> int get(int i) { return 4 + i; }
};

class Bar : public Foo {
public:
        virtual int get() { return 3; }
};

int main(int argv, char **argc) {
        Bar b;
        std::cout << b.get<char>(7) << std::endl;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

Clang 3.6,gcc 4.7,gcc 4.8和gcc 4.9都将"b.get(7)"标记为"b.get"和"char"之间的比较运算符.

template-test.cpp: In function ‘int main(int, char**)’:
template-test.cpp:16:17: error: invalid use of non-static member function
  std::cout << b.get<char>(7) << std::endl;
                 ^
template-test.cpp:16:21: error: expected primary-expression before ‘char’
  std::cout << b.get<char>(7) << std::endl;
                     ^
Run Code Online (Sandbox Code Playgroud)

(这是gcc 4.9,其他人说类似的东西)

这应该有用吗?

我找到的解决方法是在基类和派生类中声明模板化的"get".

And*_*owl 9

该名称get在派生类隐藏的名称get中的基类.因此,get()在执行名称查找时找不到函数模板,编译器只能以您看到的方式解释这些令牌.

您可以usingBar类中使用声明来修复:

class Bar : public Foo {
public:
    using Foo::get;
//  ^^^^^^^^^^^^^^^
    virtual int get() { return 3; }
};
Run Code Online (Sandbox Code Playgroud)

这是Coliru现场演示.

如果您无法修改定义,Bar因为它不在您的控制范围内,我猜您可以将调用限定为get():

std::cout << f.Foo::get<char>(7) << std::endl; // get() template is found now.
Run Code Online (Sandbox Code Playgroud)

在这里观看现场演示.另一种选择是通过指针或引用来执行调用Foo:

Bar b;
Foo& f = b;
std::cout << f.get<char>(7) << std::endl; // get() template is found now.
Run Code Online (Sandbox Code Playgroud)

再一次,活着的例子.