|
In software engineering, the curiously recurring template pattern (CRTP), is a software design pattern where a base class template is instantiated with a derived class type as its template parameter. The name of the pattern was coined by Coplien[1], who had observed it in some of the earliest C++ template code. Software engineering (SE) is the practice of creating and maintaining software applications by applying technologies and practices from engineering, computer science, project management, application domains and other fields. ...
In software engineering, design patterns are standard solutions to common problems in software design. ...
In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. ...
C++ (generally pronounced see plus plus) is a general-purpose programming language. ...
Example
// The Curiously Recurring Template Pattern (CRTP) struct derived : base<derived> { // ... }; Typically, the base class template will take advantage of the fact that member function bodies are not instantiated until long after their declarations, and will use members of the derived class within its own member functions, via the use of a static_cast, e.g.: template <class Derived> struct base { void interface() { // ... static_cast<Derived*>(this)->implementation(); // ... } }; struct derived : base<derived> { void implementation(); }; This technique achieves a similar effect to the use of virtual functions, without the costs (and some flexibility) of dynamic polymorphism.
See also Barton-Nackman is a programming trick in C++ invented by John Barton and Lee Nackman [1]. Originally, it provided a way to overload function templates when the C++ language did not support the feature. ...
References - ↑ Coplien, James O. (1995, February). "Curiously Recurring Template Patterns". C++ Report: 24–27.
|