Introducing folly::FunctionRef
authorSven Over <over@fb.com>
Tue, 27 Sep 2016 14:44:32 +0000 (07:44 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Tue, 27 Sep 2016 14:53:50 +0000 (07:53 -0700)
commit4b39d461103b3be68903e8e882d59e2f93c136ea
tree9feb6238c66b2fde862d1f0c40f652c381cba767
parent4450b4acfcc29ebce843c89fa7cd5c9c80b7eee1
Introducing folly::FunctionRef

Summary:
This commit introduces a simple function reference type, similar to
std::reference_wrapper, but the template parameter is the function
signature type rather than the type of the referenced object.

A folly::FunctionRef is cheap to construct as it contains only a
pointer to the referenced callable and a pointer to a function which
invokes the callable.

The user of FunctionRef must be aware of the reference semantics:
storing a copy of a FunctionRef is potentially dangerous and should
be avoided unless the referenced object definitely outlives the
FunctionRef object. Thus any function that accepts a FunctionRef
parameter should only use it to invoke the referenced function and
not store a copy of it. Knowing that FunctionRef itself has reference
semantics, it is generally okay to use it to reference lambdas that
capture by reference.

Reviewed By: ericniebler

Differential Revision: D3277364

fbshipit-source-id: 0a7676919cd240da5b6e1f94cadba4289e0aca28
folly/Function.h
folly/test/FunctionRefTest.cpp [new file with mode: 0644]
folly/test/Makefile.am