在不使用 decltype[c++] 的情况下传递键类型的比较函数
Pass a comparison function for Key Type without using decltype[c++]
想知道如何在不使用 decltype 的情况下将指向函数的指针作为比较函数作为键类型传递。
使用 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);
您所拥有的是声明该类型的函数指针 named pf
。
您也不需要 &
,只需 compareIsbn
即可。
想知道如何在不使用 decltype 的情况下将指向函数的指针作为比较函数作为键类型传递。
使用 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);
您所拥有的是声明该类型的函数指针 named pf
。
您也不需要 &
,只需 compareIsbn
即可。