boost - Overloading of C++ functions with similar arguments -


i'm trying create 2 overloads of function takes handler argument:

template <typename handler> void foo (handler h); 

the first overload should called if handler takes boost::asio::yield_context parameter,

template <class handler> void foo (handler h,   enable_if_t<is_same<result_of_t<handler (yield_context)>, void>::value>* = 0); 

and second 1 should called if handler takes boost::function parameter.

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); 

unfortunately not work:

main.cpp:22:3: error: call 'foo' ambiguous   foo ([] (func_type f) {});   ^~~ main.cpp:11:6: note: candidate function [with handler = (lambda @ main.cpp:22:8)] void foo (handler h,      ^ main.cpp:16:6: note: candidate function [with handler = (lambda @ main.cpp:22:8)] void foo (handler h,      ^ 1 error generated. 

interesting, code works fine std::function:

using func_type = boost::function<void (int,int)>; 

as far understand that's because boost::function has excessive overloads possible calling operators, , confuses result_of checks.

anyway, possible create "foo" overloads "distinguish" between handlers taking yield_context , handlers taking boost::function parameters?

coliru code: http://coliru.stacked-crooked.com/a/18344cd1b8364466

the standard library seems have adopted "moniker" argument approach:

unique_lock<mutex> lk(std::defer_lock, mx); 

or

std::pair<foo, foo> p1(t, t); std::pair<foo, foo> p2(std::piecewise_construct, t, t); 

or

std::async(std::launch::deferred, foo, argument1); std::async(std::launch::async, foo, argument1); 

etc.

the pattern that

struct defer_lock_t final { }; // exposition constexpr defer_lock_t defer_lock; 

these "monikers" have "unique" detectable types guaranteed not confusable/convertible etc. actual argument types.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -