gpt4 book ai didi

c++ - 空包的仿函数可变参数模板包扩展在 clang++ 和 g++ 中给出不同的结果

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:42 25 4
gpt4 key购买 nike

以下代码compiles successfully with clang++ 3.8.0 and fails to compile with g++ 7.2.0 (使用 -std=c++14 -O0 -Wall -Wextra -Werror -pedantic-errors 编译标志):

auto foo = [](auto functor, auto... argument_functors)
{
functor(argument_functors()...);
};

auto do_nothing = [](auto...) {};


int main()
{
foo(do_nothing);
}

g++ 错误信息:

main.cpp: In instantiation of '<lambda(auto:1, auto:2 ...)> [with auto:1 = <lambda(auto:3, ...)>; auto:2 = {}]':
main.cpp:11:16: required from here
main.cpp:3:9: error: no match for call to '(<lambda(auto:3, ...)>) ()'
functor(argument_functors()...);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:29: note: candidate: template<class auto:3> <lambda(auto:3, ...)>::operator decltype (((const<lambda(auto:3, ...)>*)((const<lambda(auto:3, ...)>* const)0))->operator()(static_cast<auto:3&&>(<anonymous>))) (*)(auto:3, ...)() const <deleted>
auto do_nothing = [](auto...) {};
^
main.cpp:6:29: note: template argument deduction/substitution failed:
main.cpp:3:9: note: candidate expects 1 argument, 0 provided
functor(argument_functors()...);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:29: note: candidate: template<class auto:3> <lambda(auto:3, ...)>
auto do_nothing = [](auto...) {};
^
main.cpp:6:29: note: template argument deduction/substitution failed:
main.cpp:3:9: note: candidate expects 1 argument, 0 provided
functor(argument_functors()...);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

哪些编译器行为符合标准?

最佳答案

这是 gcc 错误 64095 .

来自 [dcl.fct]/18 :

There is a syntactic ambiguity when an ellipsis occurs at the end of a parameter-declaration-clause without a preceding comma. In this case, the ellipsis is parsed as part of the abstract-declarator if the type of the parameter either names a template parameter pack that has not been expanded or contains auto; otherwise, it is parsed as part of the parameter-declaration-clause.

基本上,参数列表中的 (T...) 可以解释为:

  • T 类型的一个未命名参数和一个省略号。
  • 一个函数参数包,类型为模板参数包T

如果 T 是模板参数包或 auto,消歧规则应该优先选择后者,但 gcc 选择将其解释为省略号参数。

关于c++ - 空包的仿函数可变参数模板包扩展在 clang++ 和 g++ 中给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47762669/

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