visual c++ – Unable to use a std::map with function pointers

Why is this code failing to compile?

std::map<SetAssignNameFunc, GetAssignNameFunc> mapAssignNameFunctions =
{
    {&CChristianLifeMinistryEntry::SetVideoHost, &CChristianLifeMinistryEntry::GetVideoHost},
    {&CChristianLifeMinistryEntry::SetVideoCohost, &CChristianLifeMinistryEntry::GetVideoCohost},
    {&CChristianLifeMinistryEntry::SetChairman, &CChristianLifeMinistryEntry::GetChairman},
    {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor1, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor1},
    {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor2, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor2},
    {&CChristianLifeMinistryEntry::SetOpenPrayer, &CChristianLifeMinistryEntry::GetOpenPrayer},
    {&CChristianLifeMinistryEntry::SetTreasures1, &CChristianLifeMinistryEntry::GetTreasures1},
    {&CChristianLifeMinistryEntry::SetTreasures2, &CChristianLifeMinistryEntry::GetTreasures2},
    {&CChristianLifeMinistryEntry::SetLiving1, &CChristianLifeMinistryEntry::GetLiving1},
    {&CChristianLifeMinistryEntry::SetLiving2, &CChristianLifeMinistryEntry::GetLiving2},
    {&CChristianLifeMinistryEntry::SetLiving2b, &CChristianLifeMinistryEntry::GetLiving2b},
    {&CChristianLifeMinistryEntry::SetCBSConductor, &CChristianLifeMinistryEntry::GetCBSConductor},
    {&CChristianLifeMinistryEntry::SetCBSReader, &CChristianLifeMinistryEntry::GetCBSReader},
    {&CChristianLifeMinistryEntry::SetClosePrayer, &CChristianLifeMinistryEntry::GetClosePrayer}
};

The two definitions are:

using GetAssignNameFunc = CString (CChristianLifeMinistryEntry::*)() const;
using SetAssignNameFunc = void (CChristianLifeMinistryEntry::*)(CString);

The complete code is:

std::vector<GetAssignNameFunc> CChristianLifeMinistryEditorDlg::PreprocessAutoAssignActions(const AutoAssignActionVector &vAutoAssignActions)
{
    std::vector<GetAssignNameFunc> vpfnGetAssignNames;
    std::map<SetAssignNameFunc, GetAssignNameFunc> mapAssignNameFunctions =
    {
        {&CChristianLifeMinistryEntry::SetVideoHost, &CChristianLifeMinistryEntry::GetVideoHost},
        {&CChristianLifeMinistryEntry::SetVideoCohost, &CChristianLifeMinistryEntry::GetVideoCohost},
        {&CChristianLifeMinistryEntry::SetChairman, &CChristianLifeMinistryEntry::GetChairman},
        {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor1, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor1},
        {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor2, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor2},
        {&CChristianLifeMinistryEntry::SetOpenPrayer, &CChristianLifeMinistryEntry::GetOpenPrayer},
        {&CChristianLifeMinistryEntry::SetTreasures1, &CChristianLifeMinistryEntry::GetTreasures1},
        {&CChristianLifeMinistryEntry::SetTreasures2, &CChristianLifeMinistryEntry::GetTreasures2},
        {&CChristianLifeMinistryEntry::SetLiving1, &CChristianLifeMinistryEntry::GetLiving1},
        {&CChristianLifeMinistryEntry::SetLiving2, &CChristianLifeMinistryEntry::GetLiving2},
        {&CChristianLifeMinistryEntry::SetLiving2b, &CChristianLifeMinistryEntry::GetLiving2b},
        {&CChristianLifeMinistryEntry::SetCBSConductor, &CChristianLifeMinistryEntry::GetCBSConductor},
        {&CChristianLifeMinistryEntry::SetCBSReader, &CChristianLifeMinistryEntry::GetCBSReader},
        {&CChristianLifeMinistryEntry::SetClosePrayer, &CChristianLifeMinistryEntry::GetClosePrayer}
    };
    for (const auto& sAutoAssignAction : vAutoAssignActions)
    {
        vpfnGetAssignNames.push_back(mapAssignNameFunctions.at(sAutoAssignAction.pfnSetAssignName));
    }
}

But when I compile:

7>xstddef(117,22): error C2296: ‘<': not valid as left operand has type 'const _Ty' 7> with 7> [
7> _Ty=SetAssignNameFunc
7> ]
7>xstddef(116): message : while compiling class template member function ‘bool std::less::operator ()(const _Ty &,const _Ty &) const’ 7> with 7> [
7> _Ty=SetAssignNameFunc
7> ]
7>xutility(1453): message : see reference to function template instantiation ‘bool std::less::operator()((const _Ty &,const _Ty &) const’ being compiled 7> with 7> [
7> _Ty=SetAssignNameFunc
7> ]
7>xmemory(1360): message : see reference to class template instantiation ‘std::less’ being compiled 7>xmemory(1360): message : see reference to variable template ‘const bool is_empty_v > >)> >’ being compiled 7>map(73): message : see reference to class template instantiation ‘std: :_Treestd::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>‘ being compiled 7> with 7> [
7> _Kty=SetAssignNameFunc,
7> _Ty=GetAssignNameFunc,
7> _Pr=std::less,
7> _Alloc=std::allocator<std::pair<const SetAssignNameFunc,GetAssignNameFunc>>
7> ]
7>ChristianLifeMinistryEditorDlg.cpp(9502): message : see reference to class template instantiation ‘std::map>> being compiled 7>C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.32.31326includexstddef(117,22): error C2297: ‘<': not valid as right operand has type 'const _Ty' 7> with 7> [
7> _Ty=SetAssignNameFunc
7> ]


Update

I tried:

    std::unordered_map<SetAssignNameFunc, GetAssignNameFunc, SetAssignNameFuncHash> mapAssignNameFunctions =
    {
        {&CChristianLifeMinistryEntry::SetVideoHost, &CChristianLifeMinistryEntry::GetVideoHost},
        {&CChristianLifeMinistryEntry::SetVideoCohost, &CChristianLifeMinistryEntry::GetVideoCohost},
        {&CChristianLifeMinistryEntry::SetChairman, &CChristianLifeMinistryEntry::GetChairman},
        {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor1, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor1},
        {&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor2, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor2},
        {&CChristianLifeMinistryEntry::SetOpenPrayer, &CChristianLifeMinistryEntry::GetOpenPrayer},
        {&CChristianLifeMinistryEntry::SetTreasures1, &CChristianLifeMinistryEntry::GetTreasures1},
        {&CChristianLifeMinistryEntry::SetTreasures2, &CChristianLifeMinistryEntry::GetTreasures2},
        {&CChristianLifeMinistryEntry::SetLiving1, &CChristianLifeMinistryEntry::GetLiving1},
        {&CChristianLifeMinistryEntry::SetLiving2, &CChristianLifeMinistryEntry::GetLiving2},
        {&CChristianLifeMinistryEntry::SetLiving2b, &CChristianLifeMinistryEntry::GetLiving2b},
        {&CChristianLifeMinistryEntry::SetCBSConductor, &CChristianLifeMinistryEntry::GetCBSConductor},
        {&CChristianLifeMinistryEntry::SetCBSReader, &CChristianLifeMinistryEntry::GetCBSReader},
        {&CChristianLifeMinistryEntry::SetClosePrayer, &CChristianLifeMinistryEntry::GetClosePrayer}
    };


    for (const auto& sAutoAssignAction : vAutoAssignActions)
    {
        vpfnGetAssignNames.push_back(mapAssignNameFunctions.at(sAutoAssignAction.pfnSetAssignName));
    }

But I get an exception when running on this line:

It says:

std::out_of_range at memory location

Leave a Comment