tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 1 | # base::Callback<> and base::Bind() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 2 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 3 | ## Introduction |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 4 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 5 | The templated `Callback<>` class is a generalized function object. Together with |
| 6 | the `Bind()` function in base/bind.h, they provide a type-safe method for |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 7 | performing partial application of functions. |
| 8 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 9 | Partial application (or "currying") is the process of binding a subset of a |
| 10 | function's arguments to produce another function that takes fewer arguments. |
| 11 | This can be used to pass around a unit of delayed execution, much like lexical |
| 12 | closures are used in other languages. For example, it is used in Chromium code |
| 13 | to schedule tasks on different MessageLoops. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 14 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 15 | A callback with no unbound input parameters (`Callback<void()>`) is called a |
| 16 | `Closure`. Note that this is NOT the same as what other languages refer to as a |
| 17 | closure -- it does not retain a reference to its enclosing environment. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 18 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 19 | ### Memory Management And Passing |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 20 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 21 | The Callback objects themselves should be passed by const-reference, and stored |
| 22 | by copy. They internally store their state via a refcounted class and thus do |
| 23 | not need to be deleted. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 24 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 25 | The reason to pass via a const-reference is to avoid unnecessary AddRef/Release |
| 26 | pairs to the internal state. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 27 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 28 | ## Quick reference for basic stuff |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 29 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 30 | ### Binding A Bare Function |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 31 | |
| 32 | ```cpp |
| 33 | int Return5() { return 5; } |
| 34 | base::Callback<int()> func_cb = base::Bind(&Return5); |
| 35 | LOG(INFO) << func_cb.Run(); // Prints 5. |
| 36 | ``` |
| 37 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 38 | ### Binding A Class Method |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 39 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 40 | The first argument to bind is the member function to call, the second is the |
| 41 | object on which to call it. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 42 | |
| 43 | ```cpp |
| 44 | class Ref : public base::RefCountedThreadSafe<Ref> { |
| 45 | public: |
| 46 | int Foo() { return 3; } |
| 47 | void PrintBye() { LOG(INFO) << "bye."; } |
| 48 | }; |
| 49 | scoped_refptr<Ref> ref = new Ref(); |
| 50 | base::Callback<void()> ref_cb = base::Bind(&Ref::Foo, ref); |
| 51 | LOG(INFO) << ref_cb.Run(); // Prints out 3. |
| 52 | ``` |
| 53 | |
| 54 | By default the object must support RefCounted or you will get a compiler |
| 55 | error. If you're passing between threads, be sure it's |
| 56 | RefCountedThreadSafe! See "Advanced binding of member functions" below if |
| 57 | you don't want to use reference counting. |
| 58 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 59 | ### Running A Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 60 | |
| 61 | Callbacks can be run with their `Run` method, which has the same |
| 62 | signature as the template argument to the callback. |
| 63 | |
| 64 | ```cpp |
| 65 | void DoSomething(const base::Callback<void(int, std::string)>& callback) { |
| 66 | callback.Run(5, "hello"); |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | Callbacks can be run more than once (they don't get deleted or marked when |
| 71 | run). However, this precludes using base::Passed (see below). |
| 72 | |
| 73 | ```cpp |
| 74 | void DoSomething(const base::Callback<double(double)>& callback) { |
| 75 | double myresult = callback.Run(3.14159); |
| 76 | myresult += callback.Run(2.71828); |
| 77 | } |
| 78 | ``` |
| 79 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 80 | ### Passing Unbound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 81 | |
| 82 | Unbound parameters are specified at the time a callback is `Run()`. They are |
| 83 | specified in the `Callback` template type: |
| 84 | |
| 85 | ```cpp |
| 86 | void MyFunc(int i, const std::string& str) {} |
| 87 | base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc); |
| 88 | cb.Run(23, "hello, world"); |
| 89 | ``` |
| 90 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 91 | ### Passing Bound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 92 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 93 | Bound parameters are specified when you create the callback as arguments to |
| 94 | `Bind()`. They will be passed to the function and the `Run()`ner of the callback |
| 95 | doesn't see those values or even know that the function it's calling. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 96 | |
| 97 | ```cpp |
| 98 | void MyFunc(int i, const std::string& str) {} |
| 99 | base::Callback<void()> cb = base::Bind(&MyFunc, 23, "hello world"); |
| 100 | cb.Run(); |
| 101 | ``` |
| 102 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 103 | A callback with no unbound input parameters (`base::Callback<void()>`) is called |
| 104 | a `base::Closure`. So we could have also written: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 105 | |
| 106 | ```cpp |
| 107 | base::Closure cb = base::Bind(&MyFunc, 23, "hello world"); |
| 108 | ``` |
| 109 | |
| 110 | When calling member functions, bound parameters just go after the object |
| 111 | pointer. |
| 112 | |
| 113 | ```cpp |
| 114 | base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world"); |
| 115 | ``` |
| 116 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 117 | ### Partial Binding Of Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 118 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 119 | You can specify some parameters when you create the callback, and specify the |
| 120 | rest when you execute the callback. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 121 | |
| 122 | ```cpp |
| 123 | void MyFunc(int i, const std::string& str) {} |
| 124 | base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23); |
| 125 | cb.Run("hello world"); |
| 126 | ``` |
| 127 | |
| 128 | When calling a function bound parameters are first, followed by unbound |
| 129 | parameters. |
| 130 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 131 | ## Quick reference for advanced binding |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 132 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 133 | ### Binding A Class Method With Weak Pointers |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 134 | |
| 135 | ```cpp |
| 136 | base::Bind(&MyClass::Foo, GetWeakPtr()); |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 137 | ``` |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 138 | |
| 139 | The callback will not be run if the object has already been destroyed. |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 140 | **DANGER**: weak pointers are not threadsafe, so don't use this when passing between |
| 141 | threads! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 142 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 143 | ### Binding A Class Method With Manual Lifetime Management |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 144 | |
| 145 | ```cpp |
| 146 | base::Bind(&MyClass::Foo, base::Unretained(this)); |
| 147 | ``` |
| 148 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 149 | This disables all lifetime management on the object. You're responsible for |
| 150 | making sure the object is alive at the time of the call. You break it, you own |
| 151 | it! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 152 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 153 | ### Binding A Class Method And Having The Callback Own The Class |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 154 | |
| 155 | ```cpp |
| 156 | MyClass* myclass = new MyClass; |
| 157 | base::Bind(&MyClass::Foo, base::Owned(myclass)); |
| 158 | ``` |
| 159 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 160 | The object will be deleted when the callback is destroyed, even if it's not run |
| 161 | (like if you post a task during shutdown). Potentially useful for "fire and |
| 162 | forget" cases. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 163 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 164 | ### Ignoring Return Values |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 165 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 166 | Sometimes you want to call a function that returns a value in a callback that |
| 167 | doesn't expect a return value. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 168 | |
| 169 | ```cpp |
| 170 | int DoSomething(int arg) { cout << arg << endl; } |
| 171 | base::Callback<void(int)> cb = |
| 172 | base::Bind(base::IgnoreResult(&DoSomething)); |
| 173 | ``` |
| 174 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 175 | ## Quick reference for binding parameters to Bind() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 176 | |
| 177 | Bound parameters are specified as arguments to `Bind()` and are passed to the |
| 178 | function. A callback with no parameters or no unbound parameters is called a |
| 179 | `Closure` (`base::Callback<void()>` and `base::Closure` are the same thing). |
| 180 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 181 | ### Passing Parameters Owned By The Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 182 | |
| 183 | ```cpp |
| 184 | void Foo(int* arg) { cout << *arg << endl; } |
| 185 | int* pn = new int(1); |
| 186 | base::Closure foo_callback = base::Bind(&foo, base::Owned(pn)); |
| 187 | ``` |
| 188 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 189 | The parameter will be deleted when the callback is destroyed, even if it's not |
| 190 | run (like if you post a task during shutdown). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 191 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 192 | ### Passing Parameters As A unique_ptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 193 | |
| 194 | ```cpp |
| 195 | void TakesOwnership(std::unique_ptr<Foo> arg) {} |
| 196 | std::unique_ptr<Foo> f(new Foo); |
| 197 | // f becomes null during the following call. |
| 198 | base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f)); |
| 199 | ``` |
| 200 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 201 | Ownership of the parameter will be with the callback until the callback is run, |
| 202 | and then ownership is passed to the callback function. This means the callback |
| 203 | can only be run once. If the callback is never run, it will delete the object |
| 204 | when it's destroyed. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 205 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 206 | ### Passing Parameters As A scoped_refptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 207 | |
| 208 | ```cpp |
| 209 | void TakesOneRef(scoped_refptr<Foo> arg) {} |
| 210 | scoped_refptr<Foo> f(new Foo) |
| 211 | base::Closure cb = base::Bind(&TakesOneRef, f); |
| 212 | ``` |
| 213 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 214 | This should "just work." The closure will take a reference as long as it is |
| 215 | alive, and another reference will be taken for the called function. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 216 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 217 | ### Passing Parameters By Reference |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 218 | |
| 219 | Const references are *copied* unless `ConstRef` is used. Example: |
| 220 | |
| 221 | ```cpp |
| 222 | void foo(const int& arg) { printf("%d %p\n", arg, &arg); } |
| 223 | int n = 1; |
| 224 | base::Closure has_copy = base::Bind(&foo, n); |
| 225 | base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); |
| 226 | n = 2; |
| 227 | foo(n); // Prints "2 0xaaaaaaaaaaaa" |
| 228 | has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb" |
| 229 | has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa" |
| 230 | ``` |
| 231 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 232 | Normally parameters are copied in the closure. |
| 233 | **DANGER**: ConstRef stores a const reference instead, referencing the original |
| 234 | parameter. This means that you must ensure the object outlives the callback! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 235 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 236 | ## Implementation notes |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 237 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 238 | ### Where Is This Design From: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 239 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 240 | The design `Callback` and Bind is heavily influenced by C++'s `tr1::function` / |
| 241 | `tr1::bind`, and by the "Google Callback" system used inside Google. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 242 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 243 | ### How The Implementation Works: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 244 | |
| 245 | There are three main components to the system: |
| 246 | 1) The Callback classes. |
| 247 | 2) The `Bind()` functions. |
| 248 | 3) The arguments wrappers (e.g., `Unretained()` and `ConstRef()`). |
| 249 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 250 | The Callback classes represent a generic function pointer. Internally, it stores |
| 251 | a refcounted piece of state that represents the target function and all its |
| 252 | bound parameters. Each `Callback` specialization has a templated constructor |
| 253 | that takes an `BindState<>*`. In the context of the constructor, the static |
| 254 | type of this `BindState<>` pointer uniquely identifies the function it is |
| 255 | representing, all its bound parameters, and a `Run()` method that is capable of |
| 256 | invoking the target. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 257 | |
| 258 | `Callback`'s constructor takes the `BindState<>*` that has the full static type |
| 259 | and erases the target function type as well as the types of the bound |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 260 | parameters. It does this by storing a pointer to the specific `Run()` function, |
| 261 | and upcasting the state of `BindState<>*` to a `BindStateBase*`. This is safe as |
| 262 | long as this `BindStateBase` pointer is only used with the stored `Run()` |
| 263 | pointer. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 264 | |
| 265 | To `BindState<>` objects are created inside the `Bind()` functions. |
| 266 | These functions, along with a set of internal templates, are responsible for |
| 267 | |
| 268 | - Unwrapping the function signature into return type, and parameters |
| 269 | - Determining the number of parameters that are bound |
| 270 | - Creating the BindState storing the bound parameters |
| 271 | - Performing compile-time asserts to avoid error-prone behavior |
| 272 | - Returning an `Callback<>` with an arity matching the number of unbound |
| 273 | parameters and that knows the correct refcounting semantics for the |
| 274 | target object if we are binding a method. |
| 275 | |
| 276 | The `Bind` functions do the above using type-inference, and template |
| 277 | specializations. |
| 278 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 279 | By default `Bind()` will store copies of all bound parameters, and attempt to |
| 280 | refcount a target object if the function being bound is a class method. These |
| 281 | copies are created even if the function takes parameters as const |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 282 | references. (Binding to non-const references is forbidden, see bind.h.) |
| 283 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 284 | To change this behavior, we introduce a set of argument wrappers (e.g., |
| 285 | `Unretained()`, and `ConstRef()`). These are simple container templates that |
| 286 | are passed by value, and wrap a pointer to argument. See the file-level comment |
| 287 | in base/bind_helpers.h for more info. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 288 | |
| 289 | These types are passed to the `Unwrap()` functions, and the `MaybeRefcount()` |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 290 | functions respectively to modify the behavior of `Bind()`. The `Unwrap()` and |
| 291 | `MaybeRefcount()` functions change behavior by doing partial specialization |
| 292 | based on whether or not a parameter is a wrapper type. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 293 | |
| 294 | `ConstRef()` is similar to `tr1::cref`. `Unretained()` is specific to Chromium. |
| 295 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 296 | ### Why Not Tr1 Function/Bind? |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 297 | |
| 298 | Direct use of `tr1::function` and `tr1::bind` was considered, but ultimately |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 299 | rejected because of the number of copy constructors invocations involved in the |
| 300 | binding of arguments during construction, and the forwarding of arguments during |
| 301 | invocation. These copies will no longer be an issue in C++0x because C++0x will |
| 302 | support rvalue reference allowing for the compiler to avoid these copies. |
| 303 | However, waiting for C++0x is not an option. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 304 | |
| 305 | Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the |
| 306 | `tr1::bind` call itself will invoke a non-trivial copy constructor three times |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 307 | for each bound parameter. Also, each when passing a `tr1::function`, each bound |
| 308 | argument will be copied again. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 309 | |
| 310 | In addition to the copies taken at binding and invocation, copying a |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 311 | `tr1::function` causes a copy to be made of all the bound parameters and state. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 312 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 313 | Furthermore, in Chromium, it is desirable for the `Callback` to take a reference |
| 314 | on a target object when representing a class method call. This is not supported |
| 315 | by tr1. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 316 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 317 | Lastly, `tr1::function` and `tr1::bind` has a more general and flexible |
| 318 | API. This includes things like argument reordering by use of |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 319 | `tr1::bind::placeholder`, support for non-const reference parameters, and some |
| 320 | limited amount of subtyping of the `tr1::function` object (e.g., |
| 321 | `tr1::function<int(int)>` is convertible to `tr1::function<void(int)>`). |
| 322 | |
| 323 | These are not features that are required in Chromium. Some of them, such as |
| 324 | allowing for reference parameters, and subtyping of functions, may actually |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 325 | become a source of errors. Removing support for these features actually allows |
| 326 | for a simpler implementation, and a terser Currying API. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 327 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 328 | ### Why Not Google Callbacks? |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 329 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 330 | The Google callback system also does not support refcounting. Furthermore, its |
| 331 | implementation has a number of strange edge cases with respect to type |
| 332 | conversion of its arguments. In particular, the argument's constness must at |
| 333 | times match exactly the function signature, or the type-inference might |
| 334 | break. Given the above, writing a custom solution was easier. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 335 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame^] | 336 | ### Missing Functionality |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 337 | - Invoking the return of `Bind`. `Bind(&foo).Run()` does not work; |
| 338 | - Binding arrays to functions that take a non-const pointer. |
| 339 | Example: |
| 340 | ```cpp |
| 341 | void Foo(const char* ptr); |
| 342 | void Bar(char* ptr); |
| 343 | Bind(&Foo, "test"); |
| 344 | Bind(&Bar, "test"); // This fails because ptr is not const. |
| 345 | ``` |
| 346 | |
| 347 | If you are thinking of forward declaring `Callback` in your own header file, |
| 348 | please include "base/callback_forward.h" instead. |