Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
(Redirected from Template (programming))
Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.
Major inspirations for C++ templates were the parameterized modules provided by CLU and the generics provided by Ada.[1]
Jun 04, 2014 Templates in C. A template is a simple and yet very powerful tool in C. The simple idea is to pass data type as a parameter so that we donât need to write the same code for different data types. For example, a software company may need sort for different data types.
Templates are powerful features of C which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates. Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs. Tools-Editor Options. Use Display tab to edit font. Use Syntax tab to edit background, foreground, and text colors.
Technical overview[edit]
There are three kinds of templates: function templates, class templates and, since C++14, variable templates. Since C++11, templates may be either variadic or non-variadic; in earlier versions of C++ they are always non-variadic.
Function templates[edit]![]()
A function template behaves like a function except that the template can have arguments of many different types (see example). In other words, a function template represents a family of functions. The format for declaring function templates with type parameters is:
Both expressions have the same meaning and behave in exactly the same way. The latter form was introduced to avoid confusion,[2] since a type parameter need not be a class. (It can also be a basic type such as
int or double .)
For example, the C++ Standard Library contains the function template
max(x, y) which returns the larger of x and y . That function template could be defined like this:
This single function definition works with many data types. Specifically, it works with all data types for which > (the greater-than operator) is defined. The usage of a function template saves space in the source code file in addition to limiting changes to one function description and making the code easier to read.
A template does not produce smaller object code, though, compared to writing separate functions for all the different data types used in a specific program. For example, if a program uses both an
int and a double version of the max() function template shown above, the compiler will create an object code version of max() that operates on int arguments and another object code version that operates on double arguments. The compiler output will be identical to what would have been produced if the source code had contained two separate non-templated versions of max() , one written to handle int and one written to handle double .
Here is how the function template could be used: Dev c++ load header files.
In the first two cases, the template argument
Type is automatically deduced by the compiler to be int and double , respectively. In the third case automatic deduction of max(3, 7.0) would fail because the type of the parameters must in general match the template arguments exactly. Therefore, we explicitly instantiate the double version with max<double>() .
This function template can be instantiated with any copy-constructible type for which the expression
y > x is valid. For user-defined types, this implies that the greater-than operator (> ) must be overloaded in the type.
Class templates[edit]
A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.[3] The C++ Standard Library contains many class templates, in particular the containers adapted from the Standard Template Library, such as
vector .
Variable templates[edit]
In C++14, templates can be also used for variables, as in the following example:
Template specialization[edit]
When a function or class is instantiated from a template, a specialization of that template is created by the compiler for the set of arguments used, and the specialization is referred to as being a generated specialization.
Explicit template specialization[edit]
Sometimes, the programmer may decide to implement a special version of a function (or class) for a given set of template type arguments which is called an explicit specialization. In this way certain template types can have a specialized implementation that is optimized for the type or a more meaningful implementation than the generic implementation.
https://ladypowerful.weebly.com/entity-framework-core-generated-key-add.html. Explicit specialization is used when the behavior of a function or class for particular choices of the template parameters must deviate from the generic behavior: that is, from the code generated by the main template, or templates. For example, the template definition below defines a specific implementation of
max() for arguments of type bool :
Variadic templates[edit]
C++11 introduced variadic templates, which can take a variable number of arguments in a manner somewhat similar to variadic functions such as
std::printf . Function templates, class templates and (in C++14) variable templates can all be variadic.
Template aliases[edit]
C++11 introduced template aliases, which act like parameterized typedefs.
The following code shows the definition of a template alias
StrMap . This allows, for example, StrMap<int> to be used as shorthand for std::unordered_map<int,std::string> .
Advantages and disadvantages of templates over macros[edit]
Some uses of templates, such as the
max() function mentioned above, were previously fulfilled by function-like preprocessormacros. For example, the following is a C++ max() macro that evaluates to the maximum of its two arguments as defined by the < operator:
Both macros and templates are expanded at compile time. Macros are always expanded inline, while templates are only expanded inline when the compiler deems it appropriate. When expanded inline, macro functions and function templates have no extraneous runtime overhead. Template functions with many lines of code will incur runtime overhead when they are not expanded inline, but the reduction in code size may help the code to fit into the CPU's instruction cache.
Macro arguments are not evaluated prior to expansion. The expression using the macro defined above
Live autotune software. may evaluate to a negative number (because std::rand() will be called twice as specified in the macro, using different random numbers for comparison and output respectively), while the call to template function
will always evaluate to a non-negative number.
As opposed to macros, templates are considered type-safe; that is, they require type-checking at compile time. Hence, the compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.
By design, templates can be utilized in very complex problem spaces, whereas macros are substantially more limited.
There are fundamental drawbacks to the use of templates:
Dev C++ Program Download
Additionally, the use of the 'less than' and 'greater than' signs as delimiters is problematic for tools (such as text editors) which analyze source code syntactically. Adbfire for mac. It is difficult for such tools to determine whether a use of these tokens is as comparison operators or template delimiters. For example, this line of code:
Os x mojave dmg iso. may be a function call with two parameters, each the result of a comparison expression. Alternatively, it could be a declaration of a constructor for class
foo taking a parameter d whose type is the parameterized a < b, c > .
Generic programming features in other languages[edit]![]() Dev C++ Templates Pdf
Initially, the concept of templates was not included in some languages, such as Java and C# 1.0. Java's adoption of generics mimics the behavior of templates, but is technically different. C# added generics (parameterized types) in .NET 2.0. The generics in Ada predate C++ templates.
Although C++ templates, Java generics, and .NET generics are often considered similar, generics only mimic the basic behavior of C++ templates.[4] Some of the advanced template features utilized by libraries such as Boost and STLSoft, and implementations of the STL itself, for template metaprogramming (explicit or partial specialization, default template arguments, template non-type arguments, template template arguments, .) are not available with generics.
In C++ templates, compile-time cases were historically performed by pattern matching over the template arguments. For example, the template base class in the Factorial example below is implemented by matching 0 rather than with an inequality test, which was previously unavailable. However, the arrival in C++11 of standard library features such as std::conditional has provided another, more flexible way to handle conditional template instantiation.
With these definitions, one can compute, say 6! at compile time using the expression
Factorial<6>::value .Alternatively, constexpr in C++11 can be used to calculate such values directly using a function at compile-time. Honeywell hepa air purifier user manual.
Dev C++ Templates PrintableSee also[edit]References[edit]
External links[edit]
Dev C++ Templates Online
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Template_(C%2B%2B)&oldid=942675599'
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |