Chromium Code Reviews| Index: base/bind_internal.h |
| diff --git a/base/bind_internal.h b/base/bind_internal.h |
| index 5845a4d031de9c3df658820a4b2f4cc3e545fe61..1c3aa38feea7536e5c9d3fe8fdb50cd62313d39d 100644 |
| --- a/base/bind_internal.h |
| +++ b/base/bind_internal.h |
| @@ -422,6 +422,19 @@ struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { |
| } |
| }; |
| +// Template helpers to detect using Bind() on a base::Callback without any |
| +// additional arguments. In that case, the original base::Callback object should |
| +// just be directly used. |
| +template <typename Functor, typename... BoundArgs> |
| +struct BindingCallbackWithNoArgs { |
| + static constexpr bool value = false; |
| +}; |
| + |
| +template <typename Signature, typename... BoundArgs> |
| +struct BindingCallbackWithNoArgs<Callback<Signature>, BoundArgs...> { |
|
tzik
2016/09/20 07:33:04
Could you add a specialization for OnceCallback to
dcheng
2016/09/20 08:22:39
How come things like CancellationChecker don't cur
dcheng
2016/09/24 00:49:17
Done (though I did this by templatizing on the add
|
| + static constexpr bool value = sizeof...(BoundArgs) == 0; |
| +}; |
| + |
| // BindState<> |
| // |
| // This stores all the state passed into Bind(). |
| @@ -439,7 +452,12 @@ struct BindState final : BindStateBase { |
| : BindState(IsCancellable{}, |
| invoke_func, |
| std::forward<ForwardFunctor>(functor), |
| - std::forward<ForwardBoundArgs>(bound_args)...) {} |
| + std::forward<ForwardBoundArgs>(bound_args)...) { |
| + static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value, |
| + "Attempting to bind a base::Callback with no additional " |
| + "arguments: save a heap allocation and use the original " |
| + "base::Callback"); |
| + } |
| Functor functor_; |
| std::tuple<BoundArgs...> bound_args_; |