使用成员函数指针作为键C++映射时出现问题

Issue with C++ map using member function pointers as keys

本文关键字:映射 C++ 问题 成员 函数 指针      更新时间:2023-10-16

我正在用C++写作,尝试在 Ubuntu 下编译,并且我在使用函数指针作为键的映射时遇到了一些问题。当我定义地图时,我没有收到编译错误,但是一旦我尝试插入一个元素,我就会得到一个相当冗长的

In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from main.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = int (MyClass::*)()]’:
/usr/include/c++/4.6/bits/stl_tree.h:1277:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = int (MyClass::*)(), _Val = std::pair<int (MyClass::* const)(), std::vector<int> >, _KeyOfValue = std::_Select1st<std::pair<int (MyClass::* const)(), std::vector<int> > >, _Compare = std::less<int (MyClass::*)()>, _Alloc = std::allocator<std::pair<int (MyClass::* const)(), std::vector<int> > >]’
/usr/include/c++/4.6/bits/stl_map.h:518:41:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = int (MyClass::*)(), _Tp = std::vector<int>, _Compare = std::less<int (MyClass::*)()>, _Alloc = std::allocator<std::pair<int (MyClass::* const)(), std::vector<int> > >, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator = std::_Rb_tree_iterator<std::pair<int (MyClass::* const)(), std::vector<int> > >, std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<int (MyClass::* const)(), std::vector<int> >]’
main.cpp:36:51:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: invalid operands of types ‘int (MyClass::* const)()’ and ‘int (MyClass::* const)()’ to binary ‘operator<’

下面是导致上述错误消息的示例:

#include <iostream>
#include <map>
#include <vector>
// class definition
class MyClass
{
    public:
            int f1(void);
            int f2(void);
};
int MyClass::f1(void)
{
    return 1;
}
int MyClass::f2(void)
{
    return 2;
}
using namespace std;
int main( int argc, char* argv[] )
{
    // define map
    map< int (MyClass::*)(void), vector<int> > myMap;
    vector<int> myVector;
    //myMap[ &MyClass::f1 ] = myVector;
    myMap.insert( make_pair( &MyClass::f1, myVector) );
    return 0;
}

可能是什么问题?我尝试使用插入和 [] 分配,但出现相同的错误。浏览论坛,我发现了这个;但这可能是问题所在吗?我认为我不需要为函数指针定义运算符"<"(它们不应该表现为常规指针吗?还是我?

该错误告诉您所有您需要知道的信息:

invalid operands of types ‘int (MyClass::* const)()’ and ‘int (MyClass::* const)()’ to binary ‘operator<’

不能使用标准operator<比较成员函数指针,因此在声明映射时必须提供自定义比较器。

遗憾的是,指向成员函数的指针无法比较不相等性,因此在这种情况下,您无法定义比较运算符或使用std::map。我建议使用 std::unordered_map ,它只需要std::hash和相等比较,你可以这样做。有关哈希,请参阅此处进行相等比较。

你可以为 less实现模板专用化,如下所示:

typedef int (MyClass::*tMyClassMember)();
namespace std {
    template<>
    struct less<tMyClassMember>
    {
        bool operator()(const tMyClassMember& k1, const tMyClassMember& k2) const
        {
            auto p1 = reinterpret_cast<const intptr_t*>(&k1);
            auto p2 = reinterpret_cast<const intptr_t*>(&k2);
            return *p1 < *p2;
        }
    };
}

根据这个问题,可能有更好的方法来比较指向成员的指针,而不是将它们"强制转换"为整数,这是一个特定于实现的技巧。该问题包含有关如何执行此操作的详细信息。