参数包传递问题
Parameters pack passing issue
我不明白如何区分显式指定的第一个函数参数(下面代码中的int test
)。
有可能做到以下几点吗?
#include <utility>
struct Foo {
Foo(int i, float j):i(i),j(j){}
int i;
float j;
};
template <typename C, typename ... Args>
C createOK(Args && ... args) {
C c = C(std::forward<Args>(args) ...);
return c;
}
template <typename C, typename ... Args>
C createBad(int test, Args && ... args) {
//say here I want to do something with `test`
//and pass all the rest of the arguments to `createOK`
return createOK(std::forward<Args>(args) ...); //how do I do that?
}
int main() {
createOK<Foo>(1,2.0f); //this works
createBad<Foo>(100, 1, 2.0f); //this doesn't
return 0;
}
编译错误:
./test.cpp: In instantiation of ‘C createBad(int, Args&& ...) [with C = Foo; Args = {int, float}]’:
./test.cpp:25:32: required from here
./test.cpp:19:49: error: no matching function for call to ‘createOK(int, float)’
return createOK(std::forward<Args>(args) ...); //how do I do that?
^
./test.cpp:19:49: note: candidate is:
./test.cpp:10:3: note: template<class C, class ... Args> C createOK(Args&& ...)
C createOK(Args && ... args) {
^
./test.cpp:10:3: note: template argument deduction/substitution failed:
./test.cpp:19:49: note: couldn't deduce template parameter ‘C’
return createOK(std::forward<Args>(args) ...); //how do I do that?
请尝试使用<C>
。
相关文章:
- 传递二维数组时出现问题
- 无法安装 R 包 "nloptr"(Ubuntu 16.04 上的 R3.6.2) - C++编译器问题
- 棘手的按值传递和按引用递归问题传递
- 当我尝试通过 arduino 上的引用传递布尔数组时出现问题
- enable_if中参数包的大小问题
- 将指针传递到成员的指针,从模板参数包到函数对象
- 通过 Rcpp 和 bit64 R 包将最大的int64_t变量值从 C++ 传递到 R
- 如何将将参数包作为参数传递给其他模板类的模板类?
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 通过键入的引用问题传递函数
- 在 c++ 中使用模板传递数组时出现问题
- 与 AWS 开发工具包的链接问题
- 空构造函数和值传递问题
- 将结构传递给函数时遇到问题
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 当将参数包传递给另一个可变参数模板函数时,我是否必须使用 std::forward
- 如何将具有不同模板参数的相同结构作为参数包传递
- 将选定数量的参数包传递/转发给另一个函数
- 如何通过参数包传递引用
- 参数包传递问题