I need to pass a template class as parameter to a function but i can retrieve the typename inside the function for initialize a temporal variable
the class is declared as follows:
template <typename Type> class ListIndex_Linked
here is the inicialization of the class in the main and the call of the function
ListIndex_Linked<std::string> L;
insertion(L);
and what i'm trying to do
template <class list <typename Type>>
void insertion( list<Type>& L )
{
Type& temp = L.get(0);
{
int max = L.numElem();
for ( int i = 1, ; i < max; i++ )
{
}
}
}
but i get this error:
error: 'list' is not a template
void insertion( list<Type>& L )
^
thanks in advance for the help
In template definitions, typename provides a hint to the compiler that an unknown identifier is a type. In template parameter lists, it's used to specify a type parameter.
There is no semantic difference between class and typename in a template-parameter. typename however is possible in another context when using templates - to hint at the compiler that you are referring to a dependent type. §14.6.
Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.
A function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.
You're not declaring list
as a template template parameter
correctly.
template <template <typename> class list, typename Type>
void insertion( list<Type>& L )
{
...
}
Reference: http://en.cppreference.com/w/cpp/language/template_parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With