I'm trying to understand some C++ code. I'm an experienced Java programmer trying to learn C++. I've already read some exhaustive articles on templates, but no one of them answered me what does the following template specification mean.
template<
template<template<class> class, class> class VisualOdometryTT,
template<class> class NodeBuilderTT,
class PoseGraphT>
class VORosInterface{ ... };
The part I don't understand is template<class>
where I think some type specification is missing. But the code compiles without problems.
As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.
Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. A function/class defined using the template is called a generic function/class, and the ability to use and create generic functions/classes is one of the critical features of C++.
A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.
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.
Using NodeBuilderTT
as an example because it's easier:
NodeBuilderTT
is a template parameter that is itself a template having one parameter -- let's call that Z
.
You could have chosen to formally name Z
and the code would compile just the same:
template<class Z> class NodeBuilderTT
So far this is quite similar to declaring function arguments:
void foo(int x) {} // works
void foo(int) {} // also works
However, with the functions you would typically use the name x
inside the function body. With templates you cannot use Z
in the definition of VORosInterface
, so there is absolutely no point in naming it and it is idiomatic to write
template<class> class NodeBuilderTT
My thanks to K-ballo for helping set the record straight here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With