考虑以下代码:

struct Test {
    template <int S>
    bool call();
};

template <>
bool Test::call<0>() {
    return false;
}

template <>
bool Test::call<1>() {
    return true;
}

template <int S, typename T>
static void func(T& t) {
    t.call<S>();
}

int main()
{
    Test t;
    func<0>(t);
}

我收到一个编译错误:
a.cpp: In function ‘void func(T&)’:
a.cpp:19:15: error: expected primary-expression before ‘)’ token
a.cpp: In instantiation of ‘void func(T&) [with int S = 0; T = Test]’:
a.cpp:25:14:   required from here
a.cpp:19:5: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’

如果我将 t.call<0>()t.call<1>() 放在 main() 函数中,它就可以正常工作。有人能告诉我为什么模板参数推导不适用于这段代码吗?我不确定为什么在这种情况下传入具有部分专用模板成员函数的类型不起作用。

最佳答案

你需要说

template <int S, typename T>
static void func(T& t) {
    t.template call<S>();
}

因为T是依赖类型名,编译器不知道call()是模板函数,所以要说清楚。

关于c++ - 在类模板中调用模板成员函数时 Unresolved 重载函数类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19765182/

10-11 22:44
查看更多