Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Passing a tempate typename class as function parameter

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

like image 753
Sandlord Avatar asked May 18 '15 06:05

Sandlord


People also ask

What is typename in template?

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.

What is the difference between typename and class in template?

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.

Can we pass Nontype parameters to templates?

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.

How do you call a function template?

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.


1 Answers

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

like image 81
songyuanyao Avatar answered Nov 14 '22 22:11

songyuanyao