Allow PostTask() to take unique_ptr to classes derived of QueuedTask
Problem fixed by this CL: Let DerivedQueuedTask be a custom derivation of QueuedTask. Calling PostTask() with a std::unique_ptr<DerivedQueuedTask> does not work, because overload resolution sees PostTask(const Closure& closure) as a better match. The workaround of explicitly converting to std::unique_ptr<QueuedTask> before calling PostTask() results in less readable code.
Solution: Use std::enable_if to limit the template, thereby making the compiler use the right version of PostTask().
BUG=webrtc:8188
Review-Url: https://codereview.webrtc.org/3006933002
Cr-Commit-Position: refs/heads/master@{#19625}
diff --git a/webrtc/rtc_base/task_queue.h b/webrtc/rtc_base/task_queue.h
index e7eac2f..0d9630b 100644
--- a/webrtc/rtc_base/task_queue.h
+++ b/webrtc/rtc_base/task_queue.h
@@ -14,6 +14,7 @@
#include <list>
#include <memory>
#include <queue>
+#include <type_traits>
#if defined(WEBRTC_MAC)
#include <dispatch/dispatch.h>
@@ -189,7 +190,12 @@
// more likely). This can be mitigated by limiting the use of delayed tasks.
void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
- template <class Closure>
+ // std::enable_if is used here to make sure that calls to PostTask() with
+ // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
+ // caught by this template.
+ template <class Closure,
+ typename std::enable_if<
+ std::is_copy_constructible<Closure>::value>::type* = nullptr>
void PostTask(const Closure& closure) {
PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)));
}