C++ C++;参数包使用
我想将一个使用模板参数包和函数模板的类封装到另一个类似的类中,如以下示例所示:C++ C++;参数包使用,c++,templates,parameters,pack,C++,Templates,Parameters,Pack,我想将一个使用模板参数包和函数模板的类封装到另一个类似的类中,如以下示例所示: #include <iostream> template<typename ... Args> struct O2 { template<int T> void f() { } }; template<typename ... Args> struct O1 { O2<Args...> i2; template<
#include <iostream>
template<typename ... Args>
struct O2 {
template<int T>
void f() {
}
};
template<typename ... Args>
struct O1 {
O2<Args...> i2;
template<int T>
void g() {
i2.f<T>();
}
};
int main()
{
O1<int> i1;
i1.g<1>();
return 0;
}
#包括
模板
结构氧{
模板
void f(){
}
};
模板
结构O1{
O2-i2;
模板
void g(){
i2.f();
}
};
int main()
{
o1i1;
i1.g();
返回0;
}
但编剧说我错了:
main.cpp: In member function 'void O1<Args>::g()':
main.cpp:18:17: error: expected primary-expression before ')' token
18 | i2.f<T>();
| ^
main.cpp: In instantiation of 'void O1<Args>::g() [with int T = 1; Args = {int}]':
main.cpp:25:12: required from here
main.cpp:18:13: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
18 | i2.f<T>();
| ~~~~^~
main.cpp:在成员函数“void O1::g()”中:
main.cpp:18:17:错误:在“')标记之前应该有主表达式
18 | i2.f();
| ^
main.cpp:在“void O1::g()[with int T=1;Args={int}]”的实例化中:
main.cpp:25:12:从这里开始需要
main.cpp:18:13:错误:类型为“”且“int”到二进制“operator”的操作数无效正确语法为:
i2.template f<T>();
i2.f();
它可以工作!谢谢!