gpt4 book ai didi

c++ - 在类模板中调用模板成员函数时未解析的重载函数类型

转载 作者:太空狗 更新时间:2023-10-29 20:42:09 25 4
gpt4 key购买 nike

考虑以下代码:

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++ - 在类模板中调用模板成员函数时未解析的重载函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com