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.

667 questions
209
votes
5 answers

What is the curiously recurring template pattern (CRTP)?

Without referring to a book, can anyone please provide a good explanation for CRTP with a code example?
Alok Save
  • 190,255
  • 43
  • 403
  • 518
161
votes
7 answers

Java Enum definition

I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum: class Enum> Could someone explain how to interpret this type parameter? Bonus points for providing other examples of where a…
Dónal
  • 176,670
  • 166
  • 541
  • 787
90
votes
5 answers

CRTP to avoid dynamic polymorphism

How can I use CRTP in C++ to avoid the overhead of virtual member functions?
Amit
73
votes
5 answers

C++ static polymorphism (CRTP) and using typedefs from derived classes

I read the Wikipedia article about the curiously recurring template pattern in C++ for doing static (read: compile-time) polymorphism. I wanted to generalize it so that I could change the return types of the functions based on the derived type.…
Samuel Powell
  • 981
  • 1
  • 8
  • 8
55
votes
5 answers

invalid use of incomplete type

I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below. Does anyone know where I'm going wrong? template class A { public: //Why doesn't it like this? void…
seanhodges
  • 16,593
  • 14
  • 67
  • 89
44
votes
5 answers

Practical Uses for the "Curiously Recurring Template Pattern"

What are some practical uses for the "Curiously Recurring Template Pattern"? The "counted class" example commonly shown just isn't a convincing example to me.
Kevin
  • 22,827
  • 15
  • 51
  • 56
40
votes
3 answers

operator= and functions that are not inherited in C++?

Until a test I've just made, I believed that only Constructors were not inherited in C++. But apparently, the assignment operator= is not too... What is the reason of that ? Is there any workaround to inherit the assignment operator ? Is it…
Vincent
  • 50,257
  • 51
  • 171
  • 339
39
votes
1 answer

Why does auto return type deduction work with not fully defined types?

Consider the following: template struct Base { // NOTE: if I replace the decltype(...) below with auto, code compiles decltype(&Der::operator()) getCallOperator() const { return &Der::operator(); } }; struct Foo :…
Michał W. Urbańczyk
  • 1,393
  • 11
  • 20
32
votes
3 answers

CRTP with Protected Derived Member

In the CRTP pattern, we run into problems if we want to keep the implementation function in the derived class as protected. We must either declare the base class as a friend of the derived class or use something like this (I have not tried the…
amit
  • 19,082
  • 22
  • 86
  • 126
29
votes
7 answers

CRTP and multilevel inheritance

A friend of mine asked me "how to use CRTP to replace polymorphism in a multilevel inheritance". More precisely, in a situation like this: struct A { void bar() { // do something and then call foo (possibly) in the derived class: foo(); …
Cassio Neri
  • 17,293
  • 5
  • 43
  • 66
25
votes
6 answers

Prevent user from deriving from incorrect CRTP base

I cannot think about a proper question title to describe the problem. Hopefully the details below explains my problem clear. Consider the following code #include template class Base { public : void call () …
Yan Zhou
  • 2,481
  • 1
  • 17
  • 33
22
votes
4 answers

Protect CRTP pattern from stack overflowing in "pure virtual" calls

Consider the following standard CRTP example: #include template struct Base { void f() { static_cast(this)->f(); } void g() { static_cast(this)->g(); } }; struct Foo : public Base { …
uranix
  • 675
  • 5
  • 18
21
votes
3 answers

How to write curiously recurring templates with more than 2 layers of inheritance?

All the material I've read on Curiously Recurring Template Pattern seems to one layer of inheritance, ie Base and Derived : Base. What if I want to take it one step further? #include using std::cout; template
Kyle
  • 4,210
  • 2
  • 23
  • 44
20
votes
2 answers

Creating circular generic references

I am writing an application to do some distributed calculations in a peer to peer network. In defining the network I have two class the P2PNetwork and P2PClient. I want these to be generic and so have the definitions of: P2PNetwork
M. Jessup
  • 7,746
  • 1
  • 23
  • 29
19
votes
2 answers

Reflexive type parameter constraints: X where T : X ‒ any simpler alternatives?

Every so often I am making a simple interface more complicated by adding a self-referencing ("reflexive") type parameter constraint to it. For example, I might turn this: interface ICloneable { ICloneable Clone(); } class Sheep : ICloneable { …
stakx - no longer contributing
  • 77,057
  • 17
  • 151
  • 248
1
2 3
44 45