Reputation: 89
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
Upvotes: 5
Views: 4865
Reputation: 218323
An other way is to use auto
to not mandate Type
:
template <typename Container>
void insertion(Container& L )
{
auto& temp = L.get(0);
{
int max = L.numElem();
for ( int i = 1, ; i < max; i++ )
{
}
}
}
There is also the typedef
inside Container
which may help, something like
typename Container::reference temp = L.get(0); // or Container::value_type&
which requires something like:
template <typename Type>
class ListIndex_Linked
{
public:
using value_type = Type;
using reference = Type&;
// ...
// Your code
};
Upvotes: 1
Reputation: 173044
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
Upvotes: 6
Reputation: 227608
If insertion
is only supposed to work with ListIndex_Linked
, then you can write it as a template in terms if the list's template parameter:
template <typename Type>
void insertion(ListIndex_Linked<Type>& L)
{
...
}
Otherwise, you can use template template parameters:
template<template<class> class List, class Type>
void insertion(const List<Type>& L)
{
...
}
Upvotes: 3