Topic: Bracket, equals, bracket?
Author: Duncan Smith <dsmith1974@gmail.com>
Date: Sun, 19 Sep 2010 18:13:09 CST Raw View
In this months DDJ Herb Sutter writes some code like:
class AsyncLogFile {
public:
void write( string str ) { a.Send( [=] { log.write( str ); } ); }
My question is what does [=] mean, I thought square brackets were for
indexing arrays unless maybe the operator[] is overloaded, but then a
google codesearch shows other uses of [=], does it have a special or
common meaning?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Mon, 20 Sep 2010 01:27:12 CST Raw View
On Sep 20, 2:13 am, Duncan Smith <dsmith1...@gmail.com> wrote:
> In this months DDJ Herb Sutter writes some code like:
>
> class AsyncLogFile {
> public:
> void write( string str ) { a.Send( [=] { log.write( str ); } ); }
>
> My question is what does [=] mean, I thought square brackets were for
> indexing arrays unless maybe the operator[] is overloaded, but then a
> google codesearch shows other uses of [=], does it have a special or
> common meaning?
The meaning of [=] makes only sense in the whole context of a
so-called lambda-expression, which is a new expression form
in C++0x and defines a lambda-closure, which is a compiler-generated
class-type functor. The whole lambda-expression above is
[=] { log.write( str ); }
and defines a closure type which is - roughly said - constructed like
this:
struct __ {
std::string s;
__(const std::string& s) : s(s) {}
void operator()() {
log.write( str );
}
};
assuming that log is a global-scope variable.
Such a closure *captures* local variables, and the = within
the [] defines the default-policy, how this is done. If this
is =, all local variables are captured by *copy* (i.e. the
string s was copied. The other alternative for the default
capture is "by-reference", where you use the token & instead
of = within the [] token.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]