だから私はでおよそC++ 0xのFAQSページでBjarneにより、簡単な例をもとに、それが使用されるスコープのローカル変数にアクセスするラムダをテストしようとしていた:私はこれをしようとすると http://www2.research.att.com/~bs/C++0xFAQ.html#lambdastd :: fill()にC++ 0x lambdasローカル変数を使用するには?
簡単なテストコード:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Test std::fill() with C++0x lambda and local var
void f (int v) {
vector<int> indices(v);
int count = 0;
fill(indices.begin(), indices.end(), [&count]() {
return ++count;
});
//output test indices
for (auto x : indices) {
cout << x << endl;
}
}
int main() {
f(50);
}
私はエラーを取得する:
required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, _Tp = f(int)::<lambda()>]'
私はこのERRMSGを想定していますがのstd ::埋める()署名がconst型&を必要と示し新しい値要素の割り当てに使用します。
しかし、Bjarneの例で示されているように、この目的でfill()を使用できるようにするには、lambdaキャプチャ句の中に '[& count]'という参照を使用する必要はありません元のインデックス要素の値を 'return ++ count;」を介して増分カウントvarに再割当てすることができる。ラムダステートメントブロック?
私はこれらのラムダについてまだ完全に理解していないことを認めます! :)
残りのエラーメッセージはどこにありますか? –
Eww。 update post :) –
ここでの正確な機能は、新しいC++ 0xアルゴリズム( 'std :: iota')によって実装されていることに注意してください。 – ildjarn