1

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<std::less<void (__cdecl CChristianLifeMinistryEntry::*)(ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >)> >' 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<SetAssignNameFunc,GetAssignNameFunc,std::less,std::allocator<std::pair<const SetAssignNameFunc,GetAssignNameFunc>>>' being compiled 7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\xstddef(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 a exception when running on this line:

vpfnGetAssignNames.push_back(mapAssignNameFunctions.at(sAutoAssignAction.pfnSetAssignName));

It says:

std::out_of_range at memory location


My current approach (which works) is:

std::vector<GetAssignNameFunc> CChristianLifeMinistryEditorDlg::PreprocessAutoAssignActions(const AutoAssignActionVector &vAutoAssignActions)
{
    std::vector<GetAssignNameFunc> vpfnGetAssignNames;

    /*
    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));
    }
    */

    const auto IsProcessingAutoAssignAction = [&](SetAssignNameFunc pfnSetAssignName)
    {
        for (const S_MWB_AUTO_ASSIGN_ACTION sAutoAssignAction : vAutoAssignActions)
        {
            if (sAutoAssignAction.pfnSetAssignName == pfnSetAssignName)
            {
                return true;
            }
        }
        return false;
    };

    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetVideoHost))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetVideoHost);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetVideoCohost))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetVideoCohost);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetChairman))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetChairman);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor1))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetAuxiliaryCounsellor1);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetAuxiliaryCounsellor2))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetAuxiliaryCounsellor2);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetOpenPrayer))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetOpenPrayer);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetClosePrayer))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetClosePrayer);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetTreasures1))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetTreasures1);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetTreasures2))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetTreasures2);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetLiving1))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetLiving1);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetLiving2))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetLiving2);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetLiving2b))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetLiving2b);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetCBSConductor))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetCBSConductor);
    }
    if (!IsProcessingAutoAssignAction(&CChristianLifeMinistryEntry::SetCBSReader))
    {
        vpfnGetAssignNames.push_back(&CChristianLifeMinistryEntry::GetCBSReader);
    }

    return vpfnGetAssignNames;
}

I have debugged and it is there:

enter image description here

2
  • 1
    Pointer-to-member functions cannot be compared using the builtin less-than operator. Commented May 12, 2022 at 12:22
  • @j6t I have got round it by doing a series of if checks but the code is much longer. Commented May 12, 2022 at 12:26

2 Answers 2

1

As mentioned (or, at least, hinted at) in the comments, you will need to provide a custom comparator for your map, because the default (std::less) cannot compare pointers to functions. Exactly how you would evaluate whether or not one such function pointer is less than another is not entirely clear but, just comparing their actual values (cast to suitably sized integers), such a comparator class would look something like this for your SetAssignNameFunc type as the map's key:

struct MyCompare {
public:
    bool operator()(const SetAssignNameFunc& f1, const SetAssignNameFunc& f2) const {
        return reinterpret_cast<intptr_t>(&f1) < reinterpret_cast<intptr_t>(&f2);
    }
};

You then need to add that class as the third template parameter in your map declaration:

std::map<SetAssignNameFunc, GetAssignNameFunc, MyCompare> mapAssignNameFunctions =
{
    {&CChristianLifeMinistryEntry::SetVideoHost, &CChristianLifeMinistryEntry::GetVideoHost},
    //...

However, due to the uncertainty (undefined behaviour?) involved in using such naïve comparisons on pointers, you may find that a std::unordered_map is more robust, for your case. Such an unordered map would require only a hash function (which can return the pointer's address cast to a size_t) and, unless you have a very large number of methods to place in that map, I can't see that the lack of ordering would cause any problematical efficiency loss:

struct MyHash {
public:
    size_t operator()(const SetAssignNameFunc& f) const {
        return reinterpret_cast<size_t>(&f);
    }
};

Then use this declaration for your map:

std::unordered_map<SetAssignNameFunc, GetAssignNameFunc, MyHash> mapAssignNameFunctions =
{
    {&CChristianLifeMinistryEntry::SetVideoHost, &CChristianLifeMinistryEntry::GetVideoHost},
    //...``` 
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks. For me the order is not important. I was just wanting an easier way to locate the companion function. I’ll try these suggestions
Please see updated question.
And this mapAssignNameFunctions[sAutoAssignAction.pfnSetAssignName]; returns NULL.
@AndrewTruckle Hmm. That exception means that the call to the map's .at() function can't find a key corresponding to the given sAutoAssignAction.pfnSetAssignName value.
OK. Member function pointers are nasty creatures. Probably a clash between how one set were created versus another set. Instances, this pointers and all sorts of binding issue. I'll leave this answer here in case someone can offer a 'fix'. In the meantime, my brains are starting to turn into marmalade, just trying to figure it out.
|
0

It occurred to me that I could get around this issue by using different logic:

std::vector<GetAssignNameFunc> CChristianLifeMinistryEditorDlg::PreprocessAutoAssignActions(const AutoAssignActionVector &vAutoAssignActions)
{
    std::vector<GetAssignNameFunc> vpfnGetAssignNames;

    std::map<EnumHistAssignType, GetAssignNameFunc> mapAssignNameFunctions =
    {
        {EnumHistAssignType::Host, &CChristianLifeMinistryEntry::GetVideoHost},
        {EnumHistAssignType::CoHost, &CChristianLifeMinistryEntry::GetVideoCohost},
        {EnumHistAssignType::Chairman, &CChristianLifeMinistryEntry::GetChairman},
        {EnumHistAssignType::AuxCounsellor1, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor1},
        {EnumHistAssignType::AuxCounsellor2, &CChristianLifeMinistryEntry::GetAuxiliaryCounsellor2},
        {EnumHistAssignType::OpenPrayer, &CChristianLifeMinistryEntry::GetOpenPrayer},
        {EnumHistAssignType::Treasures1, &CChristianLifeMinistryEntry::GetTreasures1},
        {EnumHistAssignType::Treasures2, &CChristianLifeMinistryEntry::GetTreasures2},
        {EnumHistAssignType::Living1, &CChristianLifeMinistryEntry::GetLiving1},
        {EnumHistAssignType::Living2, &CChristianLifeMinistryEntry::GetLiving2},
        {EnumHistAssignType::Living3, &CChristianLifeMinistryEntry::GetLiving2b},
        {EnumHistAssignType::ConductorCBS, &CChristianLifeMinistryEntry::GetCBSConductor},
        {EnumHistAssignType::ReaderCBS, &CChristianLifeMinistryEntry::GetCBSReader},
        {EnumHistAssignType::ClosePrayer, &CChristianLifeMinistryEntry::GetClosePrayer}
    };

    for (const auto& sAutoAssignAction : vAutoAssignActions)
    {
        auto eAssignHistType = MWBAutoAssign::VectorEnumAssignmentType.at(sAutoAssignAction.iOriginalAssignindex);
        vpfnGetAssignNames.push_back(mapAssignNameFunctions.at(eAssignHistType));
    }

    return vpfnGetAssignNames;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.