<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I spoke with Marshall briefly about this at CppCon. My suggestion is that std::function should either:</div><div class=""> * correctly capture blocks and just work (as this patch does)</div><div class=""> * compile with error if you try it (and also maybe add a convenience adaptor like you suggest to allow it to manually work)</div><div class=""><br class=""></div><div class="">The current problem is that if you write something like this:</div><div class="">   template <typename F> doSomething(F&& functor) { std::function<...> capturing = std::forward<F>(functor); ... }</div><div class="">This works great for lambdas. For blocks, it compiles just fine but has runtime issues.</div><div class=""><br class=""></div><div class="">Marshall suggested to me that we just make it work, so that's the approach I've taken here. I agree it is a bit "un-pure" to add something like this into libcxx, but given that clang handles blocks natively (with -fblocks), maybe that's ok.</div><div class=""><br class=""></div><div class="">But, after looking over this patch if it doesnt feel right, I would at least suggest we add =delete/enable_if's to the right places so at least incorrect code wont compile.</div><div class=""><br class=""></div><div class="">Jared</div><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 1, 2014, at 14:57, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">[+Marshall as libc++ maintainer]<br class=""><br class="">What would it look like if we just provided a value-semantic-C++ wrapper around blocks, translating C++ move/copy operations down to the Block_copy/release/etc operations?<br class=""><br class="">That way it'd be orthogonal to the type erasure of std::function and could be used just for a convenience when using blocks in C++ code anyway:<br class=""><br class="">  auto x = make_cpp_block(/* block stuff */);<br class="">  auto y = x;<br class="">  x();<br class="">  etc... <br class=""><br class="">(& then you'd compose them with "std::function<...> f = make_cpp_block(...);" (the block wrapper could have an implicit conversion to std::function)) <br class=""><br class="">It's a little more text, but doesn't require an extension to the standard library, and can be used independently.<br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Oct 1, 2014 at 11:38 AM, Jared Grubb <span dir="ltr" class=""><<a href="mailto:jared.grubb@gmail.com" target="_blank" class="">jared.grubb@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">== Background:<br class="">
std::function will wrap anything that is callable (and can be copied/moved). Blocks are copyable and callable, however you need to use the reference counting Block_copy/Block_release functions.<br class="">
<br class="">
== Request for Comment on the Patch:<br class="">
I am working on a patch that will allow std::function to capture blocks correctly (under the right feature guards).<br class="">
<br class="">
Functionally, the patch is complete. However, it's not checkin-ready as I had a couple questions on the approach:<br class="">
 * Is the approach right? I specialized the __func storage class, as that seems to be the right thing to customize.<br class="">
 * I have included some unit test implementations. There are quite a few std::function unit tests, so I wanted to make sure I was on the right track before I do all of them.<br class="">
 * Should I add tests for ObjC++ code, under ARC and non-ARC scenarios? There are no ObjC++ files in libcxx, and the test-runner does not yet support them. Adding this is not hard, but it's a change in how things are today.<br class="">
<br class="">
If this looks like it's on the right track, I will flush out the rest of the unit tests, and optionally add ObjC++ unit tests.<br class="">
<br class="">
<br class=""><br class="">
<br class="">
Jared<br class="">
<br class="">
<br class="">_______________________________________________<br class="">
cfe-dev mailing list<br class="">
<a href="mailto:cfe-dev@cs.uiuc.edu" class="">cfe-dev@cs.uiuc.edu</a><br class="">
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>