c++ - 在不使用 decltype[c++] 的情况下传递键类型的比较函数

标签 c++ function pointers decltype multiset

想知道如何在不使用 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/

相关文章:

c++ - 将 std::allocate_shared 与多态资源分配器一起使用

c++ - "not declared in this scope"错误的问题

c - 将多维数组传递给 C 中的函数

c++ - 如何创建一个指针数组并使用 **P 将其指向 NULL?

c++ - Eigen 类取矩阵每一行的平均值(计算列 vector 的质心)

c++ - 需要帮助从类中调用函数

string - 通过返回分配的字符串导致内存泄漏

c# - 创建要在动态对象上运行的方法列表

ios - 函数调用后指针改变

c - 为什么 Clang 支持取消引用未初始化的指针