Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What does template<class> mean?

Tags:

c++

templates

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.

like image 468
Martin Pecka Avatar asked Jun 11 '13 19:06

Martin Pecka


People also ask

What does a template class do?

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.

What is a class template in C?

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++.

What does template mean in programming?

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.

How is a template class different from a template function?

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.


1 Answers

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.

like image 103
Jon Avatar answered Sep 25 '22 06:09

Jon