Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Class Template and Function Template

Tags:

c++

templates

I would like to know the difference between Class Template and Function Template and where should I use each.

like image 516
Vikram Ranabhatt Avatar asked Dec 26 '12 12:12

Vikram Ranabhatt


People also ask

What is function template and class template?

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.

What is the difference between class template and template class?

A class template is a template that is used to generate classes whereas a template class is a class that is produced by a template.

What is difference between function template and template function in C++?

Function Template is the correct terminology (a template to instantiate functions from). Template Function is a colloquial synonym. So, there's no difference whatsoever.

What is a class template?

Class templates A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.


1 Answers

When instantiated, a class template becomes a class and a function template becomes a function. Examples:

//Defines a template for a class that can hold two
//objects.
template<typename T1, typename T2>
struct pair {
    T1 first;
    T2 second;
};

//Defines a template for a function that gives the
//minimum of two values.
template<typename T>
T min(T a, T b) {
    return a < b ? a : b;
}

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.

Function templates are also able to do type-deduction, which can be useful for creating factory functions:

//Deduces the types of T1 and T2, so
//for example, a pair<int, double> can be created
//by `make_pair(10, 1.2)`
template<typename T1, typename T2>
pair<T1, T2> make_pair(T1&& t1, T2&& t2) {
    return {std::forward<T1>(t1), std::forward<T2>(t2)};
}

Class templates can be used to write programs that execute at compile-time (using types as values, and template instantiations with pattern matching as pure functions). A simple example of this is this set of class templates which removes all const from a type:

//Removes all `const` from a type, for example:
//`remove_const_recursive<int const*const volatile>::type`
//is the type `int*volatile`.
template<typename T> struct remove_const_recursive { typedef T type; };
template<typename T> struct remove_const_recursive<T const volatile> {
    typedef typename remove_const_recursive<T>::type volatile type;
};
template<typename T> struct remove_const_recursive<T volatile> {
    typedef typename remove_const_recursive<T>::type volatile type;
};
template<typename T> struct remove_const_recursive<T const> {
    typedef typename remove_const_recursive<T>::type type;
};
template<typename T> struct remove_const_recursive<T&> {
    typedef typename remove_const_recursive<T>::type& type;
};
template<typename T> struct remove_const_recursive<T*> {
    typedef typename remove_const_recursive<T>::type* type;
};

As you use templates more and more, you will realise that they can be used in a wide variety of ways. Expression templates allow you to speed up certain types of code or create domain specific languages. Template metaprogramming and tuples can be used to automatically write a variety of tedious code. You may also realise that the obtuse syntax and limited performance and semantic power of templates means that they have a cost which is not always outweighed by the benefits that they provide.

like image 126
Mankarse Avatar answered Oct 07 '22 18:10

Mankarse