C++ Function Template

Introduction

C++ function template uses rvalue reference for different function expression arguments in the higher-order function. Because it is compiled time polymorphism, the code could be optimized very well and the performance should be very good.

In this blog post, I would like to show how to use the C++ function template.

Examples

function_template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

template <typename T, typename TF>
T runBinaryFunction(T x, T y, TF&& f)
{
return f(x, y);
}

template <typename T>
T add(T x, T y)
{
return x + y;
}

template <class T>
class Add
{
public:
T operator() (T x, T y) const
{
return x + y;
}
};

template <class T>
class Calculator
{
public:
static T add(T x, T y)
{
return x + y;
}
};

template <class T>
class Calculator2
{
public:
T add(T x, T y)
{
return x + y;
}
};

int main()
{
int x = 1;
int y = 2;

// Functions
std::cout << &add<int> << std::endl; // add<int> is an lvalue expression
// Without using std::move, std::move will be implicitly added for rvalue arguments.
std::cout << runBinaryFunction(x, y, std::move(add<int>)) << std::endl;
// Functors
// std::cout << &Add<int>() << std::endl; // Error: Add<int>() is an rvalue expression
std::cout << runBinaryFunction(x, y, Add<int>()) << std::endl;
// Lambda expressions
// std::cout << &([](int x,int y)->int{return x + y;}) << std::endl; // Error: [](int x,int y)->int{return x + y;} is an rvalue expression
std::cout << runBinaryFunction(x, y, [](int x,int y)->int{return x + y;}) << std::endl;
// Static methods
std::cout << &Calculator<int>::add << std::endl; // Calculator<int>::add is an lvalue expression
// Without using std::move, std::move will be implicitly added for rvalue arguments.
std::cout << runBinaryFunction(x, y, std::move(Calculator<int>::add)) << std::endl;
// Non-static methods - failed
/*
Calculator2<int> calculator;
std::cout << runBinaryFunction(x, y, calculator.add) << std::endl;
*/
}

To compile the program, please run the following command in the terminal.

1
$ g++ function_template.cpp -o function_template -std=c++11

Using TF f would actually also work, but it would not serve as a reference. It copies the rvalues to f, which is unnecessary.

Conclusions

The application of the function template might not be as generic as std::function, but its performance should be better.

Author

Lei Mao

Posted on

02-12-2020

Updated on

02-12-2020

Licensed under


Comments