Template metaprogramming: Difference between revisions

Content deleted Content added
No edit summary
m Filled in 2 bare reference(s) with reFill 2
Line 1:
{{Cleanup bare URLs|date=September 2022}}
{{More footnotes needed|date=June 2010}}
{{Programming paradigms}}
Line 52 ⟶ 51:
To be able to use templates in this manner, the compiler must know the value of its parameters at compile time, which has the natural precondition that factorial<X>::value can only be used if X is known at compile time. In other words, X must be a constant literal or a constant expression.
 
In [[C++11]] and [[C++20]], [[constexpr]] and consteval were introduced to let the compiler execute code. Using constexpr and consteval, one can use the usual recursive factorial definition with the non-templated syntax.<ref>http{{Cite web|url=https://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html|title=Constexpr - Generalized Constant Expressions in C++11 - Cprogramming.com|website=www.cprogramming.com}}</ref>
 
==Compile-time code optimization==
Line 296 ⟶ 295:
 
==Concepts==
The C++20 standard brought C++ programmers a new tool for meta template programming, concepts.<ref>{{Cite web|url=https://en.cppreference.com/w/cpp/language/constraints|title=Constraints and concepts (since C++20) - cppreference.com|website=en.cppreference.com}}</ref>
 
[[Concepts (C++)|Concepts]] allow programmers to specify requirements for the type, to make instantiation of template possible. The compiler looks for a template with the concept that has the highest requirements.