0

Document binding of a non-const method from a const method.

Explain that a template error will occur if attempting to bind a
non-const method from a const method.

Change-Id: Ic99df287ae9a4c81b46738859ac394318815eea2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5239046
Commit-Queue: danakj <danakj@chromium.org>
Auto-Submit: Slobodan Pejic <slobodan@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1253409}
This commit is contained in:
Slobodan Pejic
2024-01-29 19:00:48 +00:00
committed by Chromium LUCI CQ
parent 7c116d889b
commit 12e1032c37

@ -383,6 +383,25 @@ error. If you're passing between threads, be sure it's RefCountedThreadSafe! See
"Advanced binding of member functions" below if you don't want to use reference
counting.
Binding a non-const method with a const object is not allowed, for example:
```cpp
class MyClass {
public:
base::OnceClosure GetCallback() const {
base::BindOnce(
// A template error will prevent the non-const method from being bound
// to the the WeakPtr<const MyClass>.
&MyClass::OnCallback,
weak_factory_.GetWeakPtr());
}
private:
void OnCallback(); // non-const
base::WeakPtrFactory<MyClass> weak_factory_{this};
}
```
### Running A Callback
Callbacks can be run with their `Run` method, which has the same signature as