Highlighting the selected overloaded function type for the given argument types

Is it possible to determine the type of function of the candidate who will choose the resolution of the overload, taking into account the set of overload and the list of arguments? For example, given:

char* f(int); int f(char*); 

I would like to write something like:

 overload<f, short>::type x; 

to declare a variable x type char* (*)(int) .

Is it possible? My first instinct was to write something like this:

 template<typename... Args> struct overload { template<typename Ret> static auto deduce(Ret (*fptr)(Args...)) -> decltype(fptr); }; 

... but this cannot handle inaccurate matches (i.e. decltype(overload<int>::deduce(f)) works, but decltype(overload<short>::deduce(f)) doesn't).

+7
c ++ c ++ 11 templates template-meta-programming overload-resolution
source share
2 answers

C ++ 14 generative lambdas for salvation:

 #define wap_function(f) \ [](auto&&... args){ return f(std::forward<decltype(args)>(args)...); } 

Note that this stone also solves the problem of first-class function templates:

 template<typename Lhs, typename Rhs> auto add(const Lhs& lhs, const Rhs& rhs) { return lhs + rhs; } std::accumulate(std::begin(array), std::end(array), 0, wrap_function(add)); 
+2
source share

This is disconnected from Convert Overloaded Function to Template Functor .

The problem is that you cannot connect an overloaded function to a template because its type must be known, so define a macro:

 #define overload_set(f, f_set) \ template <typename ...Args> \ struct f_set { \ typedef decltype(f(std::declval<Args>() ...)) return_type; \ typedef return_type (*) (Args ...) type; \ }; 

Define a structure for each function that you want to use:

 overload_set(f, f_set) 

Now the function pointer type can be obtained using:

 typedef typename f_set<int>::type f_int; typedef typename f_set<char *>::type f_charptr; 
0
source share

All Articles