Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Curiously Recurring Template Pattern

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       

      TheCuriously Recurring Template Pattern is an idiom in which a classX derives from a class templateY, taking a template parameterZ, whereY is instantiated withZ= X. For example,

      template<class Z>class Y{}; class X:public Y<X>{};

      [edit]Example

      CRTP may be used to implement "compile-time polymorphism", when a base class exposes an interface, and derived classes implement such interface.

      Run this code
      #include <cstdio> #ifndef __cpp_explicit_this_parameter // Traditional syntax template<class Derived>struct Base{void name(){static_cast<Derived*>(this)->impl();}protected:    Base()=default;// prohibits the creation of Base objects, which is UB};struct D1:public Base<D1>{void impl(){std::puts("D1::impl()");}};struct D2:public Base<D2>{void impl(){std::puts("D2::impl()");}}; #else // C++23 deducing-this syntax struct Base{void name(thisauto&& self){ self.impl();}};struct D1:public Base{void impl(){std::puts("D1::impl()");}};struct D2:public Base{void impl(){std::puts("D2::impl()");}}; #endif int main(){    D1 d1; d1.name();    D2 d2; d2.name();}

      Output:

      D1::impl()D2::impl()

      [edit]See also

      Explicit object member functions (deducingthis)(C++23)
      allows an object to create ashared_ptr referring to itself
      (class template)[edit]
      helper class template for defining aview, using thecuriously recurring template pattern
      (class template)[edit]

      [edit]External links

      1. Replace CRTP with concepts? — Sandor Drago's blog
      2. The Curiously Recurring Template Pattern (CRTP) — Sandor Drago's blog
      3. The Curiously Recurring Template Pattern (CRTP) - 1 — Fluent{C++}
      4. What the CRTP can bring to your code - 2 — Fluent{C++}
      5. An implementation helper for the CRTP - 3 — Fluent{C++}
      6. What is the Curiously Recurring Template Pattern (CRTP) — SO
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/crtp&oldid=179840"

      [8]ページ先頭

      ©2009-2025 Movatter.jp