0

What does this syntax mean?

ForExample<Something>();

Can someone explain with a few examples how and what for it can be used?

cigien
  • 57,834
  • 11
  • 73
  • 112
ClownieAdd
  • 121
  • 6

1 Answers1

2

This code could be a call of the template function or instantiation of temporary class object. For example, function is defined as:

template <typename T>
void ForExample(){
   // Do something
}

This function can be called as:

ForExample<int>();

In your case type Something can be any type (int, double, string, float ...)

Or we can define template class:

template <typename T>
class ForExample {
 // something
};

Temporary object can be created as:

ForExample<int>();
Dzemo997
  • 328
  • 2
  • 5
  • Why do I need to specify the data type in <> when calling the function? – ClownieAdd Jul 20 '21 at 18:52
  • 1
    As the name implies "template" is not the real function definition. Think about template like pattern which is used to create real function. You have to replace type T with real type (int, double, string...), type T is generic type and could be anything you want. And that's the reason you have to specify when calling function. This process is called instantiation. – Dzemo997 Jul 20 '21 at 19:10