mercredi 25 janvier 2017

How to refactor old codebase (non-static hash functions) into C++11 std::hash<>?

I have a moderate-size codebase that use a certain function name to return hash number:-

class B01{  //a class
    //..... complex thing ....
    public: size_t hashCode(){....}  
};
class B02{  //just another class
    //..... complex thing ....
    public: size_t hashCode(){....}  //This is the same name as above
};

I used it in various locations, e.g. my custom data-structure also assume that this function (hashCode) exists, roughly speaking :-

template<class T> MyArray{
    ..... inside some functions ....
    size_t hash=t->hashCode();
    .... ....
}

Today, I just found that C++11 has a new way about where the hash-function should be.

It said something like :-

namespace std
{
    template<> struct hash<S>    //S = {first_name,last_name}
    {
        typedef S argument_type;
        std::size_t operator()(argument_type const& s) const
        {
            //hash algorithm for type "S" , return "std::size_t" 
        }
    };
}

Question:

  • For long-term maintainability, should I refactor it to match C++ (or C++11) standard?
  • Should the custom hash (hashCode) be preserved?
    (It comes from my old Java habit.)
  • How to professionally refactor the code (Bxx) that has non-static hash-function (hashCode) to match C++11 standard?

My poor solution:
I have to manually insert the above not-so-tiny chunk of code into each and every class (B01,B02,...).

Aucun commentaire:

Enregistrer un commentaire