可变参数类中的模板函数
template function within variadic class
为什么goo
中的注释行无法编译?相反,我必须求助于定义全局函数hoo
而不是使用 Thing
成员函数foo
?
#include <iostream>
template <typename... T>
struct Thing {
template <typename U> void foo() {std::cout << this << 'n';}
};
template <typename U, typename... T>
void hoo (const Thing<T...>& thing) {std::cout << &thing << 'n';}
template <typename U, typename... T>
void goo (const Thing<T...>& thing) {
// thing.foo<U>(); // Why won't this line compile?
hoo<U>(thing); // Using this instead.
}
int main() {
Thing<int, double, char> thing;
goo<short>(thing);
}
需要什么更改才能改用 foo((?
thing.foo<U>()
编译器没有足够的信息来判断foo
是否是模板。 使用 template
关键字消除歧义:
thing.template foo<U>();
相关文章:
- C++ gmock - 我们如何在单元测试 cpp 文件中读取/获取 cpp 文件函数的参数值
- 访问函数变体时"Invalid conversion"错误
- 创建函数变体向量时"No matching function for call"错误
- 如何使用 SFINAE 来防止模板函数变窄?
- 如何在 c++ 中将 1 个函数变量值发送到另一个函数
- C++记录每个函数和参数值
- 谷歌模拟函数改变参数值
- 将函数形参的实参解包到c++模板类
- 函数对象应该是函数实参还是它的引用?
- 如何传递操作符作为默认函函数实参
- c++ const函数形参.有没有一种方法可以只声明函数的单个签名?
- Const正确性- Const指针作为函数实参
- 关于将指向数组的指针作为函数形参的混淆
- 将非const引用使用auto-keyword声明的lambda作为实参传递给std::函数形参类型
- 通过函数实参而不是模板形参获取元组的元素
- 在函数实参中使用模板形参不适用gcc4.8
- 作为函数形参传递的Vector初始化器列表
- 指向函数形参的指针vs函数形参
- 在模板化类和函数上下文中以默认值作为函数形参的函子:-)
- 带std::函数形参的重载操作符