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
-1
votes
1 answer

Iterator Inheritance and inheriting *this

How to write a base class and several derived classes of iterator? Does the iterator have to return itself (*this)? So far, I use typename X and static_cast(*this) to allow the derived class to inherit a function that return itself from the base…
rxu
  • 1,111
  • 1
  • 6
  • 24
-1
votes
1 answer

In this example why do I need CRTP?

See Object counter example here: Why it just does not inherit from non-template class counter. Why counter should be template? template struct counter
Narek
  • 35,407
  • 69
  • 202
  • 359
-1
votes
1 answer

Ensure safety while using CRTP

Consider following snippet of code making using of CRTP #include struct Alone { Alone() { std::cout << "Alone constructor called" << std::endl; } int me {10}; }; struct Dependant { explicit Dependant(const Alone& alone) …
skgbanga
  • 1,915
  • 1
  • 11
  • 20
-2
votes
2 answers

I'm learning the Curiously recurring template pattern. What's wrong with this template?

Thanks to this post, I'm trying to learning Curiously recurring template pattern. This is the code I wrote: #include #include #include using namespace std; template class Envelope { public: …
markzzz
  • 44,504
  • 107
  • 265
  • 458
-3
votes
2 answers

Can't get this pesky CRTPish syntax right

Consider the following code: class A { virtual void foo() = 0; }; template