C++ has an obnoxious limitation that it is impossible to pass overloaded functions to templates, for example std::max can not be nicely used with std::transform.
I was thinking that it would be nice if concepts could solve this, but in my attempts I hit the same issue. It looks like concepts are not able to constrain the template based on predicate on function type.
#include <type_traits>
#include <iostream>
#include <boost/callable_traits/args.hpp>
namespace ct = boost::callable_traits;
template <typename Fn>
concept Fn1 = std::tuple_size<ct::args_t<Fn>>::value == 1;
template <typename Fn>
concept Fn2 = std::tuple_size<ct::args_t<Fn>>::value == 2;
template<Fn1 Fn>
auto make(Fn f){
return 1;
}
template<Fn2 Fn>
auto make(Fn f){
return 2;
}
auto fn(int a){
}
auto fn(int a, float b){
return 2;
}
int main() {
std::cout << make(fn) << std::endl;
std::cout << make(fn) << std::endl;
}
notes:
- I know about different solutions to this problem, this is just an example problem to ask specifically if this can be done with concepts.
- I know that just dispatching on arity is primitive, e.g. predicate should also return
bool
, etc.