What does the expression handle<value> do in C ++?

In the context of require such brackets are clear, but I don’t quite understand what they mean here.

In principle, I understand js and a bit asm , so that can be explained on the analog.

PS : I have already started reading the book "OOP in C ++" by Laforet R., but a lot of code that looks at the same gita contains such operators, and because of them I don’t quite understand what the code does.

  • And who told you that these are operators? Templates. - isnullxbh
  • Well, I just started learning C ++, so I don’t understand all the nuances. - pnp2000
  • To tell you the truth, there is a possibility that you are now being drawn on cons. For these are not so specific things that the information about them is difficult to google and understand (in general terms). link - isnullxbh
  • but how to google if you don’t quite understand what to google ?, tried to google 'C ++' and in different combinations of '<>', 'object <val>', etc. It’s hard to google when you don’t know how to make a request. And if they are afraid of the minuses, then why bother to register here at all, and the minuses here are mostly trolling, even an article on this topic has already skipped over. - pnp2000
  • @ vnn198 Look for "C ++ Templates". In short - you can write the function min(int,int) - for two integers, the same for two double , even for two lines - in a word, for everything that can be compared. And you can write one in which the type itself acts as a variable parameter. This is the function template. There are class templates, etc., but these are details. - Harry

2 answers 2

Not knowing what a handle and value difficult to answer your question. I'll try to guess what it is about templates.

We will not dive into the dark world of metaprogramming, we will only look at the tip of the iceberg. Here is a simple example of using a pattern:

 template<class T> T summ(const T &x, const T &y){ return x + y; } 

If you call a function in code like this:

 double x = 1; double y = 2; double z = summ(x, y); 

The compiler will see that you should use double as T and create a double summ(const double &x, const double &y) function double summ(const double &x, const double &y) .

Unfortunately, the compiler cannot always understand which type should be used. In such cases, he needs help. To do this, use diamond syntax (diamond syntax). For example:

 template<class T> class MyClass{ T _variable; //... }; MyClass<int> myObject; //алмазный синтаксис 

If you go back to our first example, you can also write there:

 double x = 1; double y = 2; int z = summ<int>(x, y); 

I repeat once again, type substitution at compile time is the simplest and most superficial use of templates. The same Alexandrescu in his book is doing up with them that it becomes scary.

  • One of the code examples is where I encountered this writing here at stackoverflow.com/questions/19231174/… - pnp2000
  • @ vnn198, there is a template class. My answer is MyClass declared the same way - yrHeTaTeJlb

In C ++, an expression of the form Шаблон<Параметер 1,Праметер 2, .., Параметер N> means instantiating a template named "Template" with parameters Parameter 1, Parameter 2, and so on until Parameter N. Instantiation is a term from C ++, it means the installation of parameters into the specified template, which is close by analogy with a function call with parameters. What comes out of the substitution depends on the type of template: the function template generates functions, the type template (or class) generates new types. The resulting entities can be used in the same way as regular non-template ones. Instantiation is performed by the compiler after the preprocessor, that is, when the program is running, no templates exist, they are all deployed to specific entities.

More about C ++ templates