Using C++ lambdas and base::Callback

167 views
Skip to first unread message

Ury Zhilinsky

unread,
Aug 30, 2013, 8:07:52 PM8/30/13
to chromi...@chromium.org
Hi,

Is it possible to use the two together somehow? Ideally, I'd like to be able to do something like:

  message_loop_->PostTask(FROM_HERE, [this] () {
    // write some code
  });

Thanks,
Ury

Peter Kasting

unread,
Aug 30, 2013, 8:09:44 PM8/30/13
to uzhil...@google.com, Chromium-dev
C++11 in general is not yet permitted in the Chromium codebase.

PK 

Fred Akalin

unread,
Aug 30, 2013, 8:10:16 PM8/30/13
to uzhil...@google.com, chromium-dev
Chromium doesn't support C++11, and probably won't for a while, so the answer is currently 'no'.

When it does, it might be possible to add wrappers to convert one to the other or something.


On Fri, Aug 30, 2013 at 5:07 PM, Ury Zhilinsky <uzhil...@google.com> wrote:

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Message has been deleted

Victor Khimenko

unread,
Aug 31, 2013, 7:05:26 AM8/31/13
to uzhil...@google.com, Chromium-dev
That's what everyone wishes to do, but unfortunately it's not something C++11 can offer. You can not convert lambda which grabs some value (including "this") to any type except "auto" which means you can not put in untemplateizied class which means you can not pass it around.

There are proposal for C++14 which will probably make it possible (see here: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3574.html), but nobody knows if it'll even be adopted, let alone implemented in all compilers and become available for Chromium codebase.

David Benjamin

unread,
Aug 31, 2013, 8:14:49 AM8/31/13
to kh...@chromium.org, uzhil...@google.com, Chromium-dev
You should be able to wrap it in a std::function in C++11 though, right?

David

Victor Khimenko

unread,
Aug 31, 2013, 9:38:13 AM8/31/13
to David Benjamin, uzhil...@google.com, Chromium-dev
Sorry, my bad. You are right, of course.

Yeah, you can do that, but only if all modules are compiled as C++11. So it's just matter of waiting for the released of new toolchains, then.

Message has been deleted
Message has been deleted

Vincent Scheib

unread,
Sep 3, 2013, 1:37:11 PM9/3/13
to kh...@chromium.org, David Benjamin, Ury Zhilinsky, Chromium-dev
Ury has attempted to reply here, but the messages were deleted. So, here's a forward:

---------------------------------------------------------------------------------------
I was able to add a specialization to base::internal::RunnableAdapter to get the following syntax to work:
message_loop_->PostTask(FROM_HERE, base::Bind([this] () {
    // write some code
  }));

This also works with general purpose base::Callback-s and not just base::Clusure-s. What's interesting is that it also supports the good old functor objects and not just lambdas.

The trick is to use the type of the given lambda's operator() method to deduce the signature (search for decltype(&F::operator()) and also store the lambda itself inside of RunnableAdapter instead of a pointer to a function.

Here's a code snippet just in case that somebody is interested. Note that this is an add-on code which does not replace the existing code in any way.

namespace base { 
namespace internal {

template <typename F, typename Sig>
class RunnableAdapterFunctor;

template <typename F>
class RunnableAdapter
    : public RunnableAdapterFunctor<F, decltype(&F::operator())> {
 public:
  typedef RunnableAdapterFunctor<F, decltype(&F::operator())> BaseType;

  explicit RunnableAdapter(F f) : BaseType(f) {}
};

// Example for Arity 1
template <typename F, typename T, typename R, typename A1>
class RunnableAdapterFunctor<F, R (T::*)(A1) const>  {
 public:
  typedef R (RunType)(A1);

  RunnableAdapterFunctor(F f)
      : functor_(f) {
  }

  R Run(typename CallbackParamTraits<A1>::ForwardType a1) {
    return functor_(CallbackForward(a1));
  }

 private:
  F functor_;
};

} // namespace internal
} // namespace base


Albert J. Wong (王重傑)

unread,
Sep 4, 2013, 1:05:34 PM9/4/13
to Vincent Scheib, Victor Khimenko, David Benjamin, Ury Zhilinsky, Chromium-dev
Interesting trick.

Unfortunately, the limiting factor right now is still the lack of c++11 support in all toolchains. This is preventing us from using some other constructs that people really want (eg., nullptr, auto, for-each loops).

For lambdas, even if C++11 were fully supported, I'd be hesitant about throwing the switch 

The major thing to consider is how this interacts with Chromium smartpointers and and whether adding a second lambda-like construct (albeit the standard one) will actually help with readability.  However...until we have C++11 support, this discussion is largely theoretical.

-Albert

roc...@chromium.org

unread,
Sep 4, 2013, 1:19:36 PM9/4/13
to chromi...@chromium.org, Vincent Scheib, Victor Khimenko, David Benjamin, Ury Zhilinsky
Are roadblocks for language features or toolchains being tracked? It would be great to see metabugs like "Allow {nullptr, auto, for-each loops} in Chromium source", branching out to more specific toolchain issues that could be tackled independently by interested developers.

Nico Weber

unread,
Sep 4, 2013, 1:30:22 PM9/4/13
to roc...@chromium.org, Chromium-dev, Vincent Scheib, Victor Khimenko, David Benjamin, Ury Zhilinsky
On Wed, Sep 4, 2013 at 10:19 AM, <roc...@chromium.org> wrote:
Are roadblocks for language features or toolchains being tracked? It would be great to see metabugs like "Allow {nullptr, auto, for-each loops} in Chromium source", branching out to more specific toolchain issues that could be tackled independently by interested developers.

There's really just one issue, "turn on c++11". See the thread https://groups.google.com/a/chromium.org/forum/#!searchin/chromium-dev/c$2B$2B11/chromium-dev/oVTPsijC7d0/3hs88g_9V2oJ for things you can to do help. Ubuntu Precise ships with code that isn't valid C++11 in the dbus headers for example.

Albert J. Wong (王重傑)

unread,
Sep 4, 2013, 1:38:03 PM9/4/13
to roc...@chromium.org, Chromium-dev, Vincent Scheib, Victor Khimenko, David Benjamin, Ury Zhilinsky
On Wed, Sep 4, 2013 at 10:19 AM, <roc...@chromium.org> wrote:
Are roadblocks for language features or toolchains being tracked? It would be great to see metabugs like "Allow {nullptr, auto, for-each loops} in Chromium source", branching out to more specific toolchain issues that could be tackled independently by interested developers.

There was a huge e-mail from thakis@ sent out a while ago. I highly highly suggest starting there as the issues aren't quite "how do I make X compiler support {nullptr, auto, for-each, etc.}".

I don't think there's a meta-bug...if you're interested, maybe read the post and start one?

-Albert
Reply all
Reply to author
Forward
0 new messages