http://herbsutter.wordpress.com/2008/03/29/trip-report-februarymarch-2008-iso-c-standards-meeting/
[]와 []안에 들어가는 내용에 대해서 명쾌하게 설명을..
Herb Sutter
Re binders: Okay, I give! I’ll use a better example next time.
(no name) asked: "How are local variables captured?" You have to specify whether it’s by copy or by reference. So this example is illegal because it tries to use a local variable:
int numWidgets = 0;
for_each( v.begin(), v.end(), []( Widget& w )
{
++numWidgets; // error, numWidgets is not in scope
} );
If you want to update numWidgets directly, capture it by reference:
for_each( v.begin(), v.end(), [&numWidgets]( Widget& w )
{
++numWidgets; // increments original numWidgets
} );
// numWidgets == v.size() here
Or use the shorthand [&] to take all captured variables implicitly by reference:
for_each( v.begin(), v.end(), [&]( Widget& w )
{
++numWidgets; // increments original numWidgets
} );
// numWidgets == v.size() here
What if you want a local copy? You say to pass it by value, but for safety reasons the current proposal says you get a read-only copy that you can’t modify:
for_each( v.begin(), v.end(), [numWidgets]( Widget& w )
{
int i = numWidgets; // ok
++i;
// "++numWidgets;" would be an error
} );
// numWidgets == 0 here
Or use the shorthand [=] to take all captured variables implicitly by copy:
for_each( v.begin(), v.end(), [=]( Widget& w )
{
int i = numWidgets; // ok
++i;
// "++numWidgets;" would be an error
} );
// numWidgets == 0 here
Similarly, for the question: "What will happen in the following case:"
int flag = 0;
mypool.run( [] { flag = 1; } );
cout << flag << endl;
'Dev > C++' 카테고리의 다른 글
이미지 변환 모듈 (모바일용, GDI+) (0) | 2010.06.22 |
---|---|
C++0x, RValue Reference (0) | 2009.05.27 |
C++0x 지원 컴파일러 목록 (0) | 2009.05.20 |
C++ Refactoring (0) | 2009.04.14 |
An Overview of the Coming C++ (C++0x) Standard (0) | 2008.12.29 |