BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 14 definitions for Recurring.

Curiously recurring template pattern

Print-Friendly
About 1 pages (403 words)

Bookmark and Share Questions on this topic? Just ask!

The curiously recurring template pattern (CRTP) is a C++ idiom in which a class X derives from a class template instantiation using X itself as template argument. The name of this idiom was coined by Coplien[1], who had observed it in some of the earliest C++ template code.

Example

<source lang="cpp"> // The Curiously Recurring Template Pattern (CRTP) struct derived : base<derived> {

   // ...

}; </source> Typically, the base class template will take advantage of the fact that member function bodies (definitions) 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, or simply a cast e.g.: <source lang="cpp"> template <class Derived> struct base {

   void interface()
   {
       // ...
       static_cast<Derived*>(this)->implementation();
       // ...
   }
   static void static_func()
   {
       // ...
       Derived::static_sub_func();
       // ...
   }

}; struct derived : base<derived> {

   void implementation();
   static void static_sub_func();

}; </source> This technique achieves a similar effect to the use of virtual functions, without the costs (and some flexibility) of dynamic polymorphism. This particular use of the CRTP has been called "simulated dynamic binding" by some. This pattern is used extensively in the Windows ATL and WTL libraries. To elaborate on the above example, consider a base class with no virtual functions. Whenever the base class calls another member function, it will always call its own base class functions. When we inherit from this class, a derived class, we inherit all the member variables and member functions that weren't overridden (no constructors or destructors, of course, either). If the derived class calls an inherited function that then calls another member function, that function will never call any derived or overridden member functions in the derived class. As a result of this behavior, most C++ programmers define member functions as virtual to avoid this problem. However, if base class member functions use the CRTP pattern for all member function calls, the overridden functions in the derived class will get selected at compile time. This effectively emulates the virtual function call system at compile time without the costs in size or function call overhead (VTBL structures, and method lookups, multiple-inheritance VTBL machinery) and the slight disadvantage of not being able to do this choice at runtime.

See also

References

  • ^ Coplien, James O. (1995, February). "Curiously Recurring Template Patterns". C++ Report: 24–27.

View More Summaries on Curiously recurring template pattern
 
Ask any question on Curiously recurring template pattern and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Curiously recurring template pattern from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy