Templates
Page 1 of 1
Templates
Templates
See also: Template metaprogramming and Generic programming
C++ templates enable generic programming. C++ supports both function and class templates. Templates may be parameterized by types, compile-time constants, and other templates. C++ templates are implemented by instantiation at compile-time. To instantiate a template, compilers substitute specific arguments for a template's parameters to generate a concrete function or class instance. Some substitutions are not possible; these are eliminated by an overload resolution policy described by the phrase "Substitution failure is not an error" (SFINAE). Templates are a powerful tool that can be used for generic programming, template metaprogramming, and code optimization, but this power implies a cost. Template use may increase code size, since each template instantiation produces a copy of the template code: one for each set of template arguments. This is in contrast to run-time generics seen in other languages (e.g. Java) where at compile-time the type is erased and a single template body is preserved.
Templates are different from macros: while both of these compile-time language features enable conditional compilation, templates are not restricted to lexical substitution. Templates are aware of the semantics and type system of their companion language, as well as all compile-time type definitions, and can perform high-level operations including programmatic flow control based on evaluation of strictly type-checked parameters. Macros are capable of conditional control over compilation based on predetermined criteria, but cannot instantiate new types, recurse, or perform type evaluation and in effect are limited to pre-compilation text-substitution and text-inclusion/exclusion. In other words, macros can control compilation flow based on pre-defined symbols but cannot, unlike templates, independently instantiate new symbols. Templates are a tool for static polymorphism (see below) and generic programming.
In addition, templates are a compile time mechanism in C++ which is Turing-complete, meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram prior to runtime.
In summary, a template is a compile-time parameterized function or class written without knowledge of the specific arguments used to instantiate it. After instantiation the resulting code is equivalent to code written specifically for the passed arguments. In this manner, templates provide a way to decouple generic, broadly applicable aspects of functions and classes (encoded in templates) from specific aspects (encoded in template parameters) without sacrificing performance due to abstraction.
Outsourced Sales
Cheap Concert Tickets
meodingu- Member
- Posts : 153
BDB Credits : 421
Rep : 0
Age : 39
Location : vn hp
Job/hobbies : studient
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Fri Mar 23, 2012 11:58 am by ailsaparker15
» How can i play texan holden poker in my nokia C3-00?
Mon Nov 28, 2011 1:15 pm by Carstensen
» Pet Franchisee
Mon Nov 14, 2011 2:07 pm by ailsaparker15
» Anyone think these are good for my forum?
Mon Nov 14, 2011 12:42 pm by ailsaparker15
» hello everyone
Thu Nov 03, 2011 7:49 am by ClintonSparano
» Features of FM Antenna
Tue Oct 25, 2011 10:17 am by ailsaparker15
» Inventory Management Software for Effective Warehouse Goods Management
Fri Oct 21, 2011 7:34 am by ailsaparker15
» features of Half log siding system
Thu Oct 13, 2011 11:22 am by ailsaparker15
» Things to know before installing a solar power system.
Sat May 28, 2011 1:23 pm by JaneAlbert