C ++ Erkennungsfunktion Überladung ohne KonvertierungenC++

Programme in C++. Entwicklerforum
Anonymous
 C ++ Erkennungsfunktion Überladung ohne Konvertierungen

Post by Anonymous »

Ich habe eine Last von Funktionsüberladungen, die hauptsächlich aus zwei Signaturen besteht: < /p>
void func(const my_type1&);
void func(const my_type2&, hash_t);
< /code>
und eine Funktion, die versucht, die Funktion basierend auf der Funktion zu existieren: < /p>
template
void func_imp(const T& t, hash_t hash){
if constexpr (has_func_with_2nd_param_v){
func(t, hash);
}else{
func(t);
}
}
< /code>
, aber ich stieß auf Konvertierungsprobleme zu, die am Ende die "falsche" Überladung nennen: < /p>
#include
#include

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
struct has_func_with_2nd_param : std::false_type {};

template
struct has_func_with_2nd_param : std::true_type {};

template
constexpr bool has_func_with_2nd_param_v = has_func_with_2nd_param::value;

// I want to route to the appropriate function without conversion routing to the wrong func
template
void func_imp(const T& t, hash_t hash){
if constexpr (has_func_with_2nd_param_v){
func(t, hash);
}else{
func(t);
}
}

int main()
{
// this is true as expected
static_assert(has_func_with_2nd_param_v);

// 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);

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;
}
< /code>
Ich habe Probleme, die richtige Meta -Funktion zu finden, um das gewünschte Verhalten zu erhalten. Hat jemand Ideen?
Godbolt hier

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post