I have a function that currently takes in two template parameters. One is expected to be the smart pointer, and the other is expected to be the object type. For example, SmartPtr<MyObject> as the first template parameter and MyObject as the second template parameter.
template <typename T, typename TObject>
I would like to know whether I can determine the second parameter, MyObject, automatically from the first parameter SmartPtr<MyObject> or not so that my template function is written like this:
template <typename T>
And the type TObject in the original template function is automatically determined from T which is expected to be a smart pointer.
As requested, here is the function declaration and its use:
template <typename T, typename TObject>
T* CreateOrModifyDoc(T* doc, MyHashTable& table)
{
T* ptr = NULL;
if (!table.FindElement(doc->id, ptr))
{
table.AddElement(doc->id, new TObject());
table.FindElement(doc->id, ptr);
}
return ptr;
}
If you know that the first template parameter will be the smart pointer type, why not declare your function with only one parameter and use it as such: