想知道如何在不使用 decltype 的情况下将指向函数的指针作为比较函数作为键类型传递。
使用定义类型
std::multiset<Sales_data, decltype(compareIsbn)*> bookstore(&compareIsbn);
不使用decltype
std::multiset<Sales_data, (bool (*pf)(const Sales_data &, const Sales_data &)> bookstore(&compareIsbn);
尽管弹出错误。
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.
任何帮助将不胜感激,谢谢!
最佳答案
您不需要命名参数 - 您只需要类型:
std::multiset<Sales_data,
bool (*)(const Sales_data &, const Sales_data &)
> bookstore(&compareIsbn);
您所拥有的是声明命名 pf
类型的函数指针。
您也不需要 &
,只需 compareIsbn
即可。
关于c++ - 在不使用 decltype[c++] 的情况下传递键类型的比较函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31063978/