Pass a comparison function for Key Type without using decltype[c++]

141 views Asked by At

would like to know how I could pass a pointer to a function as a comparison function as a Key Type without using decltype.

Using decltype

std::multiset<Sales_data, decltype(compareIsbn)*> bookstore(&compareIsbn);

Without using decltype

std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> bookstore(&compareIsbn);

Though errors pop up.

    Testing.cpp:15:36: error: use of undeclared identifier 'pf'
        std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> ...
                                          ^
Testing.cpp:15:40: error: expected expression
        std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> ...
                                              ^
Testing.cpp:15:60: error: expected expression
        std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> ...
                                                                  ^
Testing.cpp:15:81: error: use of undeclared identifier 'bookstore'
        std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> bookstor...
                                                                                       ^
Testing.cpp:15:104: warning: declaration does not declare anything [-Wmissing-declarations]
  ...(bool (*pf)(const Sales_data &, const Sales_data &)> bookstore(&compareIsbn);
                                                                                 ^
1 warning and 4 errors generated.

Any help will be appreciated, thanks!

1

There are 1 answers

0
Barry On BEST ANSWER

You don't name the argument - you just want the type:

std::multiset<Sales_data, 
               bool (*)(const Sales_data &, const Sales_data &)
               > bookstore(&compareIsbn);

What you had was declaring a function pointer of that type named pf.

You also don't need the &, just compareIsbn is fine.