I started to do some reading about template, found interesting example

template <class T1, class T2>
bool GetItUP (T1 a, T2 b) 
{
   return (a>b?a:b);
}

And the way to use it:

int _tmain(int argc, _TCHAR* argv[])
{
   int a = 20;
   float b = 23.90;
   GetItUP<int,float>(a,b);
   return 0;
}

So curios enough I tried to do something like that:

template <typename T1, typename T2>
void ShowMeMap(map<T1, T2>* myTempMap, string _text, bool bIsDigit)
{
   int currentSize =  (*myTempMap).size();
   if (bIsDigit)
   {
      string temp = "AddedNewOne";
      typedef pair<int, string> ins_pair;
      (*myTempMap).insert(ins_pair(10,"NewValueInserted"));
   }
   else
   {
      //list<string> tempList ;
      //tempList.push_back("Add_1");
      //tempList.push_back("Add_2");
      //tempList.push_back("Add_3");
      //typedef pair<int, list<string>> pairTempMap;
      //(*myTempMap).insert(pairTempMap(12,&tempList));
      //(*myTempMap)[currentSize] = tempList;
   }
}

int _tmain(int argc, _TCHAR* argv[])
{
   map<int,list<string>> myComplicatedMap ;
   map<int,string> myMap ;
   typedef pair<int, list<string>> pairTempMap;

   myMap[0] = "Starts";
   myMap[1] = "ToShow";
   myMap[2] = "Values";

   list<string> textList;
   textList.push_back("One");
   textList.push_back("Two");
   textList.push_back("Three");
   textList.push_back("Four");

   myComplicatedMap.insert(pairTempMap(1,textList));
   ShowMeMap<int,string>(&myMap, "Text_Test",true);
// compiler error
//ShowMeMap <int,list<string>>(&myComplicatedMap, "Text_Teste2",false);
   return 0;
}

Try to compile I got the following:
error C2664: 'std::list<_Ty>::list(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'const std::string' to 'const std::allocator<_Ty> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility
It will be the same error if I do just like that as well:

ShowMeMap (&myComplicatedMap, "Text_Test2",false);
   return 0;
}

What I’m missing here? Can it be corrected? Is there work around (besides to make another template function)?
Regards

Hi im not to sure as that code is kind of beyond me but upon study i get a feeling maybe it is this. Your complicated map has a List of strings. in the template function you have a temp which is simply a string. Such as you add to complicated map with typedef'd pairTempMAp which has a list and that works, but the template has only a string in its map.

I cant be sure but maybe it is something to do with that, i think that type within the templace might be giving a typemismatch hence std::string to allocater ie it wants something like an iterator to add to the list in the complicated map.

Hope maybe my "not quite educated enough" guess will spark some ideas and you solve it.

Good luck :)

Edit:

Just thinking form what basic concepts i have of templates.

maybe in the template function try changing your line 8 typedef to this

typedef pair<int, string> ins_pair; //your idea
typedef pair<T1, T2> ins_pair;//this would keep the second type as a match and make this insert like your typedef'd pairTempMAp for complicated map but as int,string for the normal one

Again just another idea i have many but most are usually wrong ,... sorry if my post wrong and useless to you but i like to at least try and hopefully my silly ideas make the actual flaw appear (thats how it happened at uni a few times :P)

Edited 6 Years Ago by Kanoisa: n/a

Kanoisa,
Thanks for your replay, at this point i don't think there is wrong or right answer
Any sugestion are welcome
Cheers

did you see the edit? and try the idea of changing the typedef in your template function? if it changes the function the way i think it will i think it should work

First : Don't use pointers, as in don't do this map<T1, T2>* myTempMap.
In C++ you would use reference : map<T1, T2>& myTempMap . That way
you avoid doing stuff like (*myTempMap).blah().

Second your problem is comes from line 9, specifically this code :

(*myTempMap).insert(ins_pair(10,"NewValueInserted"));

The reason you get that compiler error is because you are inserting a string, as the second argument, where as it expects a list<string>. It doesn't matter if
bIsDigit is true or false. In template programming, the compiler
checks all code in the instantiation to make sure everything works fine. Thats
why you get that error. You can't convert string to list<string>.

Thanks firstPerson,
Very good explanation on the error, that was my understanding more less but I wasn't sure, you definitely helped.
Now I know that it is issue, is there work around? Or it is what it is , and this approach shouldn’t ever be used?

This article has been dead for over six months. Start a new discussion instead.