【发布时间】:2017-07-23 18:41:47
【问题描述】:
我目前正在使用 C/C++ MATLAB API,并且正在尝试使用可变参数模板函数;我对它不是很熟悉。它将有效地将数据结构列表输出到 MATLAB 结构中。
编译时我收到以下 2 个错误:
matLink::output2': 找不到匹配的重载函数
'void matLink::output2(mxArray *, int, const char**, const currentData &, const restData&...)': 需要 5 个参数 - 提供 3 个
template<typename... Data>
void matLink::output(const char* file, const char* varName, const char** label, const Data&... data) {
mxArray * pStruct;
// do some stuff...
output2(pStruct, 0, label, data...); // start recursion
// some more
}
template<typename currentData, typename... restData>
void matLink::output2(mxArray* pStruct, int index, const char** label, const currentData& current, const restData&... rest) {
matWrite(pStruct, current, label[index], index);
output2(pStruct, ++index, label, rest...); // both errors at this line
}
template<typename lastData>
void matLink::output2(mxArray* pStruct, int index, const char** label, const lastData& last) {
// base case
output2(pStruct, last, label[index], index);
}
我该如何解决这个问题?
【问题讨论】:
-
交换
output2定义的顺序。以错字结尾。 -
交换 output2 函数只会导致相同的错误集,但第二个错误是“...需要 4 个参数 - 提供了 3 个”?
-
我早就预料到会出现错误 - 4 参数调用似乎是模棱两可的。在这样的调用中,没有理由更喜欢第二个重载而不是第一个。此外,您的第二个重载调用
output2的方式与任一签名都不匹配 - 应该调用哪个重载?
标签: c++ templates variadic-templates