Tenho um monte de sobrecargas de funções, consistindo principalmente em duas assinaturas:
void func(const my_type1&);
void func(const my_type2&, hash_t);
e uma função que tenta chamar func com base na existência da função:
template <typename T>
void func_imp(const T& t, hash_t hash){
if constexpr (has_func_with_2nd_param_v<T>){
func(t, hash);
}else{
func(t);
}
}
mas estou tendo problemas de conversão que acabam chamando a sobrecarga 'errada':
#include <type_traits>
#include <cstdio>
struct hash_t{
};
struct my_type1{
my_type1() {}
};
struct my_type2{
my_type2() {}
my_type2(const my_type1&){}
};
void func(const my_type1&){
printf("no hash\n");
}
void func(const my_type2&, hash_t=hash_t()){
printf("hash\n");
}
template<typename T, typename = void>
struct has_func_with_2nd_param : std::false_type {};
template<typename T>
struct has_func_with_2nd_param<T, std::void_t<decltype(func(std::declval<T>(), std::declval<hash_t>()))>> : std::true_type {};
template<typename T>
constexpr bool has_func_with_2nd_param_v = has_func_with_2nd_param<T>::value;
// I want to route to the appropriate function without conversion routing to the wrong func
template <typename T>
void func_imp(const T& t, hash_t hash){
if constexpr (has_func_with_2nd_param_v<T>){
func(t, hash);
}else{
func(t);
}
}
int main()
{
// this is true as expected
static_assert(has_func_with_2nd_param_v<my_type2>);
// this is also true, since my_type2 is constructable from my_type1 and then finds finds func(my_type2, hash_t)
static_assert(has_func_with_2nd_param_v<my_type1>);
func_imp(my_type1(), hash_t()); // prints "hash", but wanting it to call the "no hash" version
func_imp(my_type2(), hash_t()); // prints "hash"
return 0;
}
Estou com dificuldades para encontrar a metafunção correta para obter o comportamento desejado. Alguém tem alguma ideia?
godbolt aqui