dimanche 27 décembre 2020

C++ primer 5th ed. class template specialization. an example case: specializing std::hash

In c++ primer 5th ed. class template specialization I have this example:

Because hash<Sales_data> uses the private members of Sales_data, we must make this class a friend of Sales_data:

template <class T> class std::hash;  // needed for the friend declaration
class Sales_data {
    friend class std::hash<Sales_data>;
    // other members as before
};

The code doesn't compile and complains:

error: invalid use of template-name ‘std::hash’ without an argument list
note: class template argument deduction is only available with ‘-std=c++17’ or ‘std=gnu++17’

I am compiling against C++11 because the book is old so that it doesn't support c++17 and 20.

If I comment out that declaration the code works fine but I am not sure whether doing so is OK and how to fix that problem.

I've tried this and worked but I am not sure whether I am doing the right thing:

   class Sales_data; // forward declaration
   template <>
   class std::hash<Sales_data>; // specialization declaration

Aucun commentaire:

Enregistrer un commentaire