What are the considerations for the untyped parameters of C++ function template?
This article shows you what the C++ function template non-type parameters pay attention to, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Template application in C++ programming language is a very important application technology. So today, let's first understand some basic concepts about the untyped parameters of the C++ function template, which is easy for you to understand.
C++ function template untyped parameters are mainly used to provide an operation constant for the function. With regard to untyped function template parameters, there are the following examples:
/ / function template definition template T addValue (T const& x) {return x + VAL;} / / other code / / the use of function templates std::transform (source.begin (), source.end (), dest.begin (), (int (*) (int const&)) addValue)
A function template is defined in the above code to add a specified int of 5 to the passed parameter. Such functions are commonly used in situations where a group of data is processed at the same time. For example, 12 lines. It should be noted here that a std::transform function itself is a template function, and its * parameter can pass a function pointer.
Therefore, the (int (*) (int const&)) addValue is actually a pointer to the instantiated addValue template function. As for how to read this pointer, please give me some advice. It is also important to note that a parameter of std::transform does not have to be a template function. Any function can (for a correct understanding of std::transform, refer to the comments below). It's just that template functions are more suitable for dealing with many types of data.
Restrictions on untyped parameters of C++ function templates.
Restrictions on untyped template parameters currently remember that it can be a constant integer (including enumerated types) and pointers to externally connected objects. For historical reasons, floating-point types cannot be used as parameters for untyped templates; pointers and strings are conditional as parameters for untyped templates. I think it has something to do with the scope and life cycle of variables. There will be more trusting introductions later in the book, so we'll take a closer look at it.
What are the above points for attention of C++ function template non-type parameters? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.