Difference between Class Template and Function Template

34,455

Solution 1

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.

Solution 2

Function templates attempt to deduce the type of specialization from the argument types.

Function templates can’t be partially specialized while classes can.

Function templates can’t have default template parameters while classes can.

Share:
34,455
Vikram Ranabhatt
Author by

Vikram Ranabhatt

Current Company: Staff Software Engineer at GE Healthcare Past Company: Tech Mahindra, Logitech, Aspire System and Marlabs Current Technology: Python(App and Backend),AWS(Glue, EMR, Dynamodb),Kubernetes, Elastic search, Parquet,ansible Spark, Pyspark and pandas Past Experience: C++,C#,MFC,ACE Framework. TCP/IP,STUN,P2P,Bonjour,WiFI Technology.

Updated on June 17, 2020

Comments

  • Vikram Ranabhatt
    Vikram Ranabhatt almost 4 years

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