samedi 14 décembre 2019

Defining custom map comparator with non stardard signature

Assuming that there is a map that has a pointer as a key. To deep compare of the underling object it's possible to write custom comparator in following way:

#include <map>
#include <memory>

bool compare(std::unique_ptr<int> lhs, std::unique_ptr<int> rhs){
    return *lhs<*rhs;
}

int main(){
    std::map<std::unique_ptr<int>, short, bool (*)(std::unique_ptr<int>, std::unique_ptr<int>)> elements(compare);
}

That's undestandable, but why would I need to explicitly pass type of the compare callback as it is imposed by the type of map key? Why it's not possible create a map in following way as the signature of it should be known:

std::map<std::unique_ptr<int>, short> elements(compare);

Are there any cases when it's needed to define a compare function with different signature?

Aucun commentaire:

Enregistrer un commentaire