views:

128

answers:

1

I'm trying to use a for_each loop in my code, but I'm getting the following error:

cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &'

here's the offending code:

typedef stdext::hash_map<
        std::string, std::list<DefaultTestContext>
    > CompleteTestList;

static void RunMappedTests(pair<string, list<DefaultTestContext>>& tests)
{
    RunAllTestsInList(tests.second);
}

void RunTestsInParallel(CompleteTestList& testList)
{
    for_each(testList.begin(), testList.end(), RunMappedTests);
}

Of course, the easy fix is to change RunMappedTests's parameter to be pass-by-value instead of pass-by-reference. Unfortunately, in this case, it comes with a huge performance penalty. It will have to copy a string, AND a list of ~64 byte data blocks. The amount of copying done is scary. I also NEED to modify the original elements in the map.

Anyone dealt with this before? Any easy fixes I don't know about?

+2  A: 

std::map<T1, T2>::value_type is std::pair<const T1, T2>. Change the parameter of RunMappedTests to pair<const string, list<DefaultTestContext>> &.

MSN
Right, but the iterator should be dereferencing into a non-const reference. I feel this is an unsolution.
GMan
It worked. Therefore, win.
Clark Gaebel
As long as the key is const it maintains the map's invariants.
MSN
@wowus: Eh, seems hackish. I'm curious if you get the same problems with a normal `map`.
GMan
Actually, it makes perfect sense. You can't modify the first element, and the string SHOULD be const. I need to read documentation closer next time. THANK YOU, OH GREAT MSN :)
Clark Gaebel
MSalters