#include <functional> struct Test { void func(int) {} }; void test() { std::function deduced = &Test::func; // compiler error std::function<void (Test *, int)> ex = &Test::func; // OK }

std::function

test

std::function

namespace std { // warning: undefined behavior template<class R, class C, class... ArgTypes> function(R(C::*)(ArgTypes...)) -> function<R(C*, ArgTypes...)>; } void test() { std::function deduced = &Test::func; // Now, OK std::function<void (Test *, int)> ex = &Test::func; Test test; deduced(&test, 10); // Calls test.func(10) ex(&test, 10); // Calls test.func(10) }

std

Arbitrary Choice

std::function

Test

Test&

PtrCallable and RefCallable

namespace callable { template <class... Args> struct PtrCallable : std::function<Args...> { using std::function<Args...>::function; // inherited constructors }; template <class... Args> struct RefCallable : std::function<Args...> { using std::function<Args...>::function; // inherited constructors }; // Deduction guides for the newly introduced types template<class R, class C, class... ArgTypes> PtrCallable(R(C::*)(ArgTypes...)) -> PtrCallable<R(C*, ArgTypes...)>; template<class R, class C, class... ArgTypes> RefCallable(R(C::*)(ArgTypes...)) -> RefCallable<R(C&, ArgTypes...)>; } // namespace callable

using namespace callable; void test() { RefCallable deduced1 = &Test::func; PtrCallable deduced2 = &Test::func; Test test; deduced1(test, 10); // Calls test.func(10) deduced2(&test, 10); // Calls (&test)->func(10) }

More deduction guides for const and volatile

namespace callable { template<class R, class C, class... ArgTypes> PtrCallable(R(C::*)(ArgTypes...) const) -> PtrCallable<R(const C*, ArgTypes...)>; template<class R, class C, class... ArgTypes> RefCallable(R(C::*)(ArgTypes...) const) -> RefCallable<R(const C&, ArgTypes...)>; // ditto for volatile and const volatile } // namespace callable

Deduction guides for rvalue-ref qualified member functions

namespace callable { template<class R, class C, class... ArgTypes> RefCallable(R(C::*)(ArgTypes...) &&) -> RefCallable<R(C&&, ArgTypes...)>; template<class R, class C, class... ArgTypes> RefCallable(R(C::*)(ArgTypes...) const &&) -> RefCallable<R(const C&&, ArgTypes...)>; // ditto for volatile and const volatile } // namespace callable

Conclusion

const Test getConstTest() { return {}; } void test_function() { std::function<void(Test*, int)> f1 = &Test::Nonconst; std::function<void(const Test*, int)> f2 = &Test::Const; Test a; const Test c; f1(&a, 1); f2(&c, 1.0); } void test_refcallable() { using namespace callable; Test a; const Test c; RefCallable f1 = &Test::Nonconst; f1(a, 1); RefCallable f2 = &Test::Const; f2(a, 1); f2(c, 1); RefCallable f3 = &Test::rvalue; f3(Test{}, 10); RefCallable f4 = &Test::rvalue_const; f4(getConstTest(), 10); } void test_ptrcallable() { using namespace callable; Test a; const Test c; PtrCallable f1 = &Test::Nonconst; f1(&a, 1); PtrCallable f2 = &Test::Const; f2(&a, 1); f2(&c, 1); }

The objective is to allow the following to be valid C++.With class template argument deduction in C++17, type arguments forabove should have been deduced. The first line in function fails to compile as of this writing becausedoes not appear to have deduction guides for conversion from pointer to member functions. On the other hand, explicitly specifying the template arguments makes the compiler happy.The following deduction guide seems to fix the issue.However, the above code is technically undefined behavior because a user-defined deduction guide for a standard library type is added innamespace. Clang does not like it either. Gcc may start rejecting this code in (near) future.Weirdly, deduction guides must be defined in the same scope where the primary template is defined. That means, writing custom deduction guides for standard library types is out of question. Not sure how I feel about that.There's another problem. In the deduction guide above, I made an arbitrary choice that the deducedis callable with a pointer to. That's arbitrary. What aboutI added two types that specify how the function will be called. This time in my own namespace.Now the choice is not arbitrary. User can pick one.More deduction guides are needed to support other varieties of member functions. Let's add them.Even more deduction guides are needed for rvalue-ref qualified member functionsWith a dozen or so new deduction guides all the following cases work. Runnable code is shared on Wandbox