gpt4 book ai didi

c++ - 不是最后一个模板参数的推导和参数包 : is this valid code?

转载 作者:太空狗 更新时间:2023-10-29 22:58:24 26 4
gpt4 key购买 nike

考虑以下最小示例:

template<typename... A, typename R>
void f(A&&..., R(*)(A...)) {}

void g(int, char) {}

int main() {
f<int, char>(42, 'c', g);
}

参数包A不是最后一个参数。
无论如何,我认为 R 可以从函数指针中推导出来,因为 [temp.param]/11 .

海湾合作委员会 (6.2) agrees with me在此之上,同时 clang (3.9) rejects the code并说它无法推断模板参数 R

是我的预期错误,因此 GCC 不应该接受它,还是 clang 的问题?

最佳答案

从提到的段落中:

If a template-parameter of a class template, variable template, or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameter of a primary class template, primary variable template, or alias template is a template parameter pack, it shall be the last template-parameter. A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list ([dcl.fct]) of the function template or has a default argument ([temp.deduct]). A template parameter of a deduction guide template ([temp.deduct.guide]) that does not have a default argument shall be deducible from the parameter-type-list of the deduction guide template. [ Example:

template<class T1 = int, class T2> class B;   // error

// U can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { } // error
template<class... T, class U> void g() { } // error

— end example ]

在这种情况下,template<typename... A, typename R>不能从函数模板的参数类型列表中推导出来,因为:

void f(A&&..., R(*)(A...))

A&&...是贪心的,会消耗g作为A&&不是 R(*)(A...)

说话迂腐,如temp.deduct.call-1 :

For a function parameter pack that occurs at the end of the parameter-declaration-list, deduction is performed for each remaining argument of the call, taking the type P of the declarator-id of the function parameter pack as the corresponding function template parameter type. Each deduction deduces template arguments for subsequent positions in the template parameter packs expanded by the function parameter pack. When a function parameter pack appears in a non-deduced context ([temp.deduct.type]), the type of that parameter pack is never deduced.

temp.deduct.type#9一样:

If P has a form that contains <T> or <i>, then each argument Pi of the respective template argument list of P is compared with the corresponding argument Ai of the corresponding template argument list of A. If the template argument list of P contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context. If Pi is a pack expansion, then the pattern of Pi is compared with each remaining argument in the template argument list of A.

并且无法指定,如您在第三个示例中所见。

当拒绝它时,clang 是正确的。

关于c++ - 不是最后一个模板参数的推导和参数包 : is this valid code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com