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

How to code a union using a struct as template, while having that union as an element

I am trying to construct a union that will replace the pair of pointers (left & right) by a union which is accessible as an array. Originally this is working code for a Binary Search Tree (BST) I want to be able to do this: p =…
Karl
  • 36
  • 5
-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,369
  • 1
  • 11
  • 29
-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
  • 38,779
  • 79
  • 233
  • 389
-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
  • 2,477
  • 2
  • 20
  • 29
-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
  • 47,390
  • 120
  • 299
  • 507
-4
votes
2 answers

Can't get this pesky CRTPish syntax right

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