简体   繁体   English

参数包被推导忽略

[英]Parameter pack ignored by deduction

Why can't my gcc-5.4.0 not deduce parameter packs if they do not appear in the end of the argument list of a function? 如果我的gcc-5.4.0没有出现在函数的参数列表的末尾,为什么不能推断出它们呢? While the call to works is deduced in a correct way way to works<int,int,int> , the call to fails is not deduced but instead only an empty parameter pack is assumed. 虽然该呼叫works以正确的方式方法是推导出works<int,int,int>调用fails是不是推断,而是假定只有一个空的参数包。 Leading to an error message about too many provided arguments for the function. 导致错误消息,提示该函数提供了太多参数。

#include <iostream>

template <typename...args_t>
void works (int first, args_t...args) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

template <typename...args_t, typename last_t, typename=void>
void fails (args_t...args, last_t last) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

int main () {
    works (0, 1, 2, 3);
    fails (0, 1, 2, 3);
    return 0;
}

EDIT: as the answers explained, it is not allowed to have typenames after the parameter packs. 编辑:作为答案解释,不允许在参数打包后使用类型名。 But according to cppreference.com , it should be valid to have other template parameters after it, if they may be deduced. 但是根据cppreference.com ,如果可以推断出其他模板参数,那么后面应该有其他模板参数是有效的。 Apparently the given example does not compile with my gcc . 显然,给定的示例无法与我的gcc一起gcc Instead it stays with the same error about too many given arguments. 取而代之的是,对于太多给定的参数,它仍然存在相同的错误。

#include <iostream>
template <typename...args_t, typename U, typename=void>
static int valid (args_t...args, U u) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    return u;
}

int main () {
    return valid(0, 0.0, -1, 3u);
}

Why can't my gcc-5.4.0 not deduce parameter packs if they do not appear in the end of the argument list of a function? 如果我的gcc-5.4.0没有出现在函数的参数列表的末尾,为什么不能推断出它们呢?

Because you cannot specify any types beyond the variadic parameter pack like that 因为您不能指定像可变参数包之外的任何类型

 void fails (args_...args, int last)
                      // ^^^^^^^^^^

It's merely the same problem as with default parameter values, or plain ellipsis ( ... ) that those need to be open towards the end of the parameter list (or say they're required to be the last element): 与默认参数值或普通省略号( ... )只是一个问题,这些问题需要在参数列表的末尾打开(或者说它们必须是最后一个元素):

 void fails(int x = 0, int last);
 void fails(int x, ..., int last);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM