Lambda表达式:term不会评估为接受1个参数的函数。

huangapple go评论66阅读模式
英文:

Lambda expression:term does not evaluate to a function taking 1 arguments

问题

我遇到了这个错误:错误 C2064:term does not evaluate to a function taking 1 arguments。

#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> ivec{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
    vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
    for_each(it, rend, [it]()->void {cout << *it << " "; });

    cout << endl;
    return 0;
}

这段代码有什么问题?有人可以告诉我问题出在哪吗?

英文:

I got this error:error C2064: term does not evaluate to a function taking 1 arguments

#include &lt;iterator&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

using namespace std;

int main()
{
	vector&lt;int&gt; ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
	vector&lt;int&gt;::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
	for_each(it, rend, [it]()-&gt;void {cout &lt;&lt; *it &lt;&lt; &quot; &quot;; });

	cout &lt;&lt; endl;
	return 0;
}

What wrong with these code?Can someone tell me where the problem is?

答案1

得分: 0

for_each 会将 *it 作为引用传递给 lambda 函数,您无需捕获迭代器并解引用它。以下代码展示了这一点:

#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
    vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
    for_each(it, rend, [](const int &i)->void {cout << i << " "; });

    cout << endl;
    return 0;
}

该代码的输出如预期的那样,是反转后的向量:

89 55 34 21 13 8 5 3 2 1 1

对于我们中的语言专家,这在 ISO 标准中得到了覆盖(我正在看的是 C++20)。请特别参考注释 2(我的强调部分),其中以迂回的方式说明迭代器被解引用并将该值传递给函数:

23.5.4 For each [alg.foreach]

template<class InputIterator, class Function>
constexpr Function for_each(
    InputIterator first,
    InputIterator last,
    Function f);
  1. 要求:Function 必须满足 Cpp17MoveConstructible 要求(表 25)。[注意:Function 不必满足 Cpp17CopyConstructible 的要求(表 26)。— 结束注释]
  2. 效果:将 f 应用于范围 [first, last) 中每个迭代器的解引用结果,从 first 开始,到 last - 1 结束。[注意:如果 first 的类型满足可变迭代器的要求,f 可能通过解引用的迭代器应用非常数函数。— 结束注释]
  3. 返回:f
  4. 复杂度:f 正好应用 last - first 次。
  5. 备注:如果 f 返回结果,结果将被忽略。
英文:

The for_each will pass *it to the lambda function as a reference, you do not need to capture the iterator and dereference it. The following code shows this in action:

#include &lt;iterator&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

using namespace std;

int main()
{
    vector&lt;int&gt; ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
    vector&lt;int&gt;::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
    for_each(it, rend, [](const int &amp;i)-&gt;void {cout &lt;&lt; i &lt;&lt; &quot; &quot;; });

    cout &lt;&lt; endl;
    return 0;
}

The output of that is, as expected, the reversed vector:

89 55 34 21 13 8 5 3 2 1 1

For the language lawyers amongst us, this is covered in [alg.foreach] in the ISO standard (C++20 is the one I'm looking at). See in particular note 2 (my emphasis) where it states, in a roundabout way, that the iterator is de-referenced and that value is passed to the function:

>23.5.4 For each [alg.foreach]
>
>template&lt;class InputIterator, class Function&gt;<br>
>constexpr Function for_each(<br>
>&nbsp;&nbsp;&nbsp;InputIterator first,<br>
>&nbsp;&nbsp;&nbsp;InputIterator last,<br>
>&nbsp;&nbsp;&nbsp;Function f);
>
> 1. Requires: Function shall satisfy the Cpp17MoveConstructible requirements (Table 25). [Note: Function need not meet the requirements of Cpp17CopyConstructible (Table 26). — end note]
> 2. Effects: Applies f to the result of dereferencing every iterator in the range [first, last), starting from first and proceeding to last - 1. [Note: If the type of first satisfies the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator. — end note]
> 3. Returns: f.
> 4. Complexity: Applies f exactly last - first times.
> 5. Remarks: If f returns a result, the result is ignored

huangapple
  • 本文由 发表于 2023年3月7日 10:52:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定