我正在尝试创建以处理程序作为参数的函数的两个重载:
template <typename Handler>
void foo (Handler h);
如果handler将boost :: asio :: yield_context作为参数
,则应调用第一个重载template <class Handler>
void foo (Handler h,
enable_if_t<is_same<result_of_t<Handler (yield_context)>, void>::value>* = 0);
如果handler将boost :: function作为参数,那么应该调用第二个。
using func_type = boost::function<void(int,int)>;
template <typename Handler>
void foo (Handler h,
enable_if_t<is_same<result_of_t<Handler (func_type)>, void>::value>* = 0);
不幸的是,这不起作用:
main.cpp:22:3: error: call to 'foo' is ambiguous
foo ([] (func_type f) {});
^~~
main.cpp:11:6: note: candidate function [with Handler = (lambda at main.cpp:22:8)]
void foo (Handler h,
^
main.cpp:16:6: note: candidate function [with Handler = (lambda at main.cpp:22:8)]
void foo (Handler h,
^
1 error generated.
有趣,但代码可以正常使用std :: function:
using func_type = boost::function<void (int,int)>;
据我所知,因为boost :: function对所有可能的调用操作符都有过多的重载,这会混淆result_of检查。
无论如何,是否有可能创建“foo”重载,可以“区分”使用yield_context的处理程序和使用boost :: function作为参数的处理程序?
Coliru代码:http://coliru.stacked-crooked.com/a/18344cd1b8364466
答案 0 :(得分:1)
标准库似乎采用了“绰号”论证方法:
unique_lock<mutex> lk(std::defer_lock, mx);
或
std::pair<Foo, Foo> p1(t, t);
std::pair<Foo, Foo> p2(std::piecewise_construct, t, t);
或
std::async(std::launch::deferred, foo, argument1);
std::async(std::launch::async, foo, argument1);
等
模式是
struct defer_lock_t final { }; // for exposition
constexpr defer_lock_t defer_lock;
这些“绰号”具有“独特的”可检测类型,保证不会与您的实际参数类型混淆/可转换等。