The objective is to allow the following to be valid C++.
The following deduction guide seems to fix the issue.
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.
I added two types that specify how the function will be called. This time in my own namespace.
More deduction guides for
More deduction guides are needed to support other varieties of member functions. Let's add them.
#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 }With class template argument deduction in C++17, type arguments for
std::function
above should have been deduced. The first line in function test
fails to compile as of this writing because std::function
does 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.
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) }However, the above code is technically undefined behavior because a user-defined deduction guide for a standard library type is added in
std
namespace. 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.
Arbitrary Choice
There's another problem. In the deduction guide above, I made an arbitrary choice that the deducedstd::function
is callable with a pointer to Test
. That's arbitrary. What about Test&
?
PtrCallable
and RefCallable
I added two types that specify how the function will be called. This time in my own namespace.
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 callableNow the choice is not arbitrary. User can pick one.
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
More deduction guides are needed to support other varieties of member functions. Let's add them.
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
Even more deduction guides are needed for rvalue-ref qualified member functionsnamespace 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
With a dozen or so new deduction guides all the following cases work. Runnable code is shared on Wandbox.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); }
Comments