-
Template question
I started to do some reading about template, found interesting example
Code:
template <class T1, class T2>
bool GetItUP (T1 a, T2 b)
{
return (a>b?a:b);
}
And the way to use it:
Code:
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:
Code:
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:
Code:
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
-
As far as I understand you're trying to insert a pair of type <int, string> and it works fine for the first call of ShowMeMap but the second call expects <int, list<string> > however you're still inserting a pair of type <int, string>.
I think you're over complicating this, anyway I think this could be solved with template specialization but maybe someone can give a better answer.
-
Thanks sugarfree,
Yes, you understood correctly my intentions
It might be overcomplicated, it is not the problem I'm trying to solve, this is more my curiosity
If it is not the way to use it - fine,
once again, thanks
-
I believe it's crapping out on this line in your ShowMeMap() function:
Code:
(*myTempMap).insert(ins_pair(10,"NewValueInserted"));
because you're inserting a pair<int, string>, but your map is a map<int, list<string> >
Edit: Nevermind, I think that's what sugarfree said above.
-
Why do you always use (*x).foo instead of x->foo? For that matter, why do you pass a pointer instead of a reference?
-
Btw, you don't need to specify the type...
ShowMeMap <int,list<string>>(&myComplicatedMap, "Text_Teste2",false);
...can be:
ShowMeMap(&myComplicatedMap, "Text_Teste2",false);
The compiler will figure out the type.
And use references instead of pointers.