Content deleted Content added
No edit summary |
Citation bot (talk | contribs) m Alter: title, isbn. Add: title-link, date. Removed parameters. Some additions/deletions were actually parameter name changes. | You can use this bot yourself. Report bugs here. | Activated by User:Neko-chan | Category:Software design patterns | via #UCB_Category |
||
Line 1:
The '''curiously recurring template pattern''' ('''CRTP''') is an idiom in [[C++]] in which a class <code>X</code> derives from a class [[Template (C++)|template]] instantiation using <code>X</code> itself as template argument.<ref>{{cite book | first1=David | last1=Abrahams | authorlink1=David Abrahams (computer programmer) | first2=Aleksey | last2=Gurtovoy | title=C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond |publisher=Addison-Wesley | isbn=0-321-22725-5| date=January 2005 }}</ref> More generally it is known as '''F-bound polymorphism''', and it is a form of [[F-bounded quantification|''F''-bounded quantification]].
==History==
The technique was formalized in 1989 as "''F''-bounded quantification."<ref>{{cite web|url=http://staff.ustc.edu.cn/~xyfeng/teaching/FOPL/lectureNotes/CookFBound89.pdf|title=F-Bounded Polymorphism for Object-Oriented Programming|author=William Cook|date=1989|display-authors=etal}}</ref> The name "CRTP" was independently coined by [[Jim Coplien]] in 1995,<ref>{{cite journal | author=Coplien, James O. | title=Curiously Recurring Template Patterns | journal=C++ Report |
as well as in code examples that [[Timothy Budd]] created in his multiparadigm language Leda.<ref>{{cite book | first=Timothy | last=Budd | authorlink=Timothy Budd | title=
The Microsoft Implementation of CRTP in [[Active Template Library]] (ATL) was independently discovered, also in 1995 by Jan Falkin who accidentally derived a base class from a derived class. Christian Beaumont first saw Jan's code and initially thought it couldn't possibly compile in the Microsoft compiler available at the time. Following this revelation that it did indeed work, Christian based the entire ATL and [[Windows Template Library]] (WTL) design on this mistake.{{Citation needed|date=August 2018}}
Line 22:
</source>
Some use cases for this pattern are [[Template metaprogramming#Static polymorphism|static polymorphism]] and other metaprogramming techniques such as those described by [[Andrei Alexandrescu]] in ''[[Modern C++ Design]]''.<ref>{{cite book | first=Andrei | last=Alexandrescu | authorlink=Andrei Alexandrescu | title=
It also figures prominently in the C++ implementation of the [[Data, Context, and Interaction]] paradigm.<ref>{{cite book | first1=James | last1=Coplien | authorlink1=James Coplien | first2=Gertrud | last2=Bjørnvig | title=
== Static polymorphism ==
Line 65:
== Object counter ==
The main purpose of an object counter is retrieving statistics of object creation and destruction for a given class.<ref>{{cite journal | author=Meyers, Scott | title=Counting Objects in C++ | journal=C/C++ Users Journal |
<source lang="cpp">
Line 160:
This happens because 'print' is a function of the base - 'Printer' - and then it returns a 'Printer' instance.
The CRTP can be used to avoid such problem and to implement "Polymorphic chaining":<ref>{{cite web|last1=Arena|first1=Marco|title=Use CRTP for polymorphic chaining|url=https://marcoarena.wordpress.com/2012/04/29/use-crtp-for-polymorphic-chaining/|accessdate=15 March 2017|date=29 April 2012}}</ref>
<source lang="cpp>
|