Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Template (C++)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Generic programming features in other languages == Initially, the concept of templates was not included in some languages, such as [[Java (programming language)|Java]] and [[C Sharp (programming language)|C#]] 1.0. [[Generics in Java|Java's adoption of generics]] mimics the behavior of templates, but is technically different. C# added generics (parameterized types) in [[.NET Framework|.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 {{nowrap|C++}} templates.<ref>{{cite web| url=http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx| title=Differences Between C++ Templates and C# Generics (C# Programming Guide)| date=12 March 2024}}</ref> Some of the advanced template features utilized by libraries such as [[Boost C++ Libraries|Boost]] and [[STLSoft C++ Libraries|STLSoft]], and implementations of the STL, for [[template metaprogramming]] (explicit or partial specialization, default template arguments, template non-type arguments, template template arguments, ...) are unavailable 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. <syntaxhighlight lang="cpp"> // Induction template<unsigned N> struct Factorial { static constexpr unsigned value = N * Factorial<N - 1>::value; }; // Base case via template specialization: template<> struct Factorial<0> { static constexpr unsigned value = 1; }; </syntaxhighlight> With these definitions, one can compute, say 6! at compile time using the expression <code>Factorial<6>::value</code>. Alternatively, <code>[[C++11#constexpr β Generalized constant expressions|constexpr]]</code> in C++11 / <code>if constexpr</code> in C++17 can be used to calculate such values directly using a function at compile-time: <syntaxhighlight lang="cpp"> template<unsigned N> unsigned factorial() { if constexpr(N<=1) return 1; else return N * factorial<N-1>(); } </syntaxhighlight> Because of this, template meta-programming is now mostly used to do operations on types.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Template (C++)
(section)
Add topic