【发布时间】:2015-03-20 18:30:12
【问题描述】:
我正在尝试创建以处理程序作为参数的函数的两个重载:
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);
如果处理程序将 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 作为参数的处理程序?
【问题讨论】:
标签: c++ boost sfinae boost-function