What is a block in C++? [duplicate]

2019-05-19 23:21发布

问题:

Possible Duplicate:
What is (double (^)(int))foofoo

I've tried searching for a definition on Google and SO and I found examples in which they are used but not clearly defined. By "block" I mean the caret symbol (^). I found it on a site where cdecl they described:

(double (^)(int, long long )) foo

as

cast foo into block(int, long long) returning double

I've never once seen this symbol used before today. Can anyone clearly describe what a block is and include with it a minimal working example? Thanks.

回答1:

Blocks are a non-standard extension to the C (and not only to the Objective-C) language by Apple. They realize closures (lambda functions, etc., however you call them) - basically they're unnamed function-like entities, enclosing code that can be called. They facilitate writing for example event-driven code, where callbacks are used exhaustively.