在类模板中调用模板成员函数时未解析的重载函数类型
Unresolved overloaded function type while calling template member function in a class template
请考虑以下代码:
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<’
如果我在main()
函数中放入t.call<0>()
或t.call<1>()
,它可以正常工作。 有人可以告诉我为什么模板参数推断不适用于此代码吗? 我不确定为什么在这种情况下传递具有部分专用模板成员函数的类型不起作用。
你需要说
template <int S, typename T>
static void func(T& t) {
t.template call<S>();
}
由于T
是依赖类型名称,因此编译器不知道call()
是模板函数,因此您必须明确说明。
您需要
使用 template
关键字消除解析的歧义:
template <int S, typename T>
static void func(T& t)
{
t.template call<S>();
}
看起来你想写
t. template call<S>();
名称call
显然是依赖的,因此,除非明确声明它是模板,否则不被视为模板。目前,我无法轻松检查这是否是唯一的问题。
相关文章:
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- c++:可变模板和函数重载
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 推断模板化函数中的函数重载
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 可变参数泛型 lambda 和函数重载
- C++中的函数重载和继承
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- C/C++ 可变参数宏函数重载
- 将基类的成员函数重载到其他派生类C++
- C++ 函数重载匹配
- C++函数重载,具体步骤是什么
- C++:使用 param pack 显式调用函数重载
- 隐式生成的函数重载用于右值参数?
- 使用函数重载输入运算符
- 运算符重载函数上的函数重载