Questions tagged [crtp]

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 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 Jim Coplien, who had observed it in some of the earliest C++ template code.

Typical use cases include static polymorphism and polymorphic copy construction (cloning).

Wikipedia.

758 questions
10
votes
3 answers

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is…
Doug
  • 8,780
  • 2
  • 27
  • 37
9
votes
1 answer

What is the best way to create a 'using' declaration involving members of incomplete types?

I have a very simple CRTP skeleton structure that contains just one vector and a private accessor in the base class. There is a helper method in the CRTP class to access it. #include template class CRTPType { // Will be…
Chuu
  • 4,301
  • 2
  • 28
  • 54
9
votes
1 answer

Passing overloaded CRTP class member method to lambda

Consider this: template struct base_t { auto& f(int x) { return (T&)*this; } auto& f(char x) { return (T&)*this; } }; struct derived_t : base_t { }; void test() { derived_t instance; auto lambda =…
Hi - I love SO
  • 615
  • 3
  • 14
9
votes
5 answers

How to implement a compile-time check that a downcast is valid in a CRTP?

I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template class Base { void MethodToOverride() { // generic stuff here } void…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
9
votes
2 answers

Why does only one of these CRTP patterns compile?

Consider the following two pieces of code with a CRTP pattern: template struct Base1 { int baz(typename Derived::value_type) { return 42; } }; struct Foo1 : Base1 { using value_type = int; }; template…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
9
votes
1 answer

C++ Low latency Design: Function Dispatch v/s CRTP for Factory implementation

As part of a system design, we need to implement a factory pattern. In combination with the Factory pattern, we are also using CRTP, to provide a base set of functionality which can then be customized by the Derived classes. Sample code below:…
Sid
  • 129
  • 1
  • 9
9
votes
1 answer

Mixing policy-based design with CRTP in C++

I'm attempting to write a policy-based host class (i.e., a class that inherits from its template class), with a twist, where the policy class is also templated by the host class, so that it can access its types. One example where this might be…
Eitan
  • 862
  • 1
  • 7
  • 17
9
votes
3 answers

Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

One of the main benefits of virtual in C++ is being able to use the base class (pointer or reference) to call derived methods. I'm reading up on using CRTP to implement static polymorphism, but I can't understand how to achieve what I've mentioned…
MGA
  • 1,658
  • 15
  • 28
9
votes
1 answer

Ensure derived class implements static method

I want to ensure, that a derived class implements a specific static method. I think doing so should be possible using static_assert, std::is_same, decltype, CRTP and maybe making use of SFINAE. However, similar code I found so far is quite complex…
sigy
  • 2,408
  • 1
  • 24
  • 55
9
votes
2 answers

Derived curiously recurring templates and covariance

Suppose I have a base class which cloning of derived classes: class Base { public: virtual Base * clone() { return new Base(); } // ... }; I have a set of derived classes which are implemented using…
Www
  • 322
  • 2
  • 13
9
votes
4 answers

C++: what is the Curiously-Recurring-Template-Pattern? and can Curiously-Recurring-Template-Pattern replace virtual functions?

I don't have a precise description of the problem so I'm just asking if this is possible (and if it is, some other information would be great). A programmer told me you can avoid runtime overhead caused by virtual functions/polymorphism. He said to…
Marco A.
  • 43,032
  • 26
  • 132
  • 246
8
votes
1 answer

Can you use constraints on derived classes in CRTP methods?

Is something like this valid C++20 code? #include template concept impls_decrement = requires(T it) { it.decrement(); }; template struct iterator_facade { Derived& operator--() requires…
Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
8
votes
1 answer

Benefits of CRTP over an abstract class?

I am new to the concept of a 'Curiously Recurring Template Pattern', and I'm reading about its potential use cases here. In that article, the author describes a simple case where we have two or more classes with some generic functionality: class A…
John O'brien
  • 321
  • 1
  • 9
8
votes
1 answer

Alternatives to CRTP in Java

The CRTP pattern allows to emulate the so called self types in Java, e. g.: abstract class AbstractFoo> implements Comparable { @Override public final int compareTo(final SELF o) { // ... …
Bass
  • 4,977
  • 2
  • 36
  • 82
8
votes
3 answers

why Curiously Recurring Template Pattern (CRTP) works

I meet a lot of explanations about what CRTP is, but there is no explanation of why it works. The Microsoft Implementation of CRTP in ATL was independently discovered, also in 1995 by Jan Falkin who accidentally derived a base class from a derived…
Soup Endless
  • 439
  • 3
  • 10