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
7
votes
2 answers

Why does CRTP not cause infinite nesting?

I am confused about how CRTP is compiled. If we have something like this: template class Base { }; class Derived : public Base { }; Why doesn't something akin to this happen during compilation? (X[Y] denotes X inherits from…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
7
votes
1 answer

Create alias for a list of types and passing it as a template parameter

I am using variadic templates to implement the visitor pattern: template class Visitor; template class Visitor { public: virtual void visit(Type &visitable) = 0; }; template
Nikopol
  • 1,091
  • 1
  • 13
  • 24
7
votes
1 answer

Compiler error when using CRTP with static_assert

Consider the following code: template struct Base { static constexpr int x_base = Derived::x_derived; //static_assert(x_base > 1, "Oops"); }; struct Derived : public Base { static constexpr int x_derived = 5…
toth
  • 2,519
  • 1
  • 15
  • 23
7
votes
3 answers

How to force use of curiously recurring template pattern in C++

I have the following base template class. template class Base { public: void do_something() { } }; It is intended to be used as a curiously recurring template pattern. It should be inherited like class B : public Base. It…
Hot.PxL
  • 1,902
  • 1
  • 17
  • 30
7
votes
2 answers

Should members variables used by the CRTP base type be in the derived type?

I've been learning about CRTP (Curiously Recurring Template Pattern) today and believe I understand it well enough. However, in the examples I've seen the state is stored in the derived type, even though the base type relies upon their presence. To…
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
7
votes
2 answers

Compile errors for simple CRTP case with dependent types

I'm trying to use a simple form of CRTP (Curiously Recurring Template Pattern) as I've got several classes, each with several related classes, and I want a means of binding them together (eg I've got classes like Widget, Doobry and Whatsit, with…
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
7
votes
1 answer

Use of member of derived type in CRTP class

I have a curiously recurring template pattern class and a derived class like so: template class A { typedef typename Derived::C D; D x; }; class B : public A { public: class C { }; }; This fails to compile due to B not being…
Dylan
  • 1,692
  • 1
  • 13
  • 24
7
votes
1 answer

C++ - circular dependence (using inner type of subclass in templated base class)

I run into problem with circular dependence in a templated class. There is a code sample: template struct A { typedef typename T::C D; //typename T::C c; }; struct B : public A { struct C {}; }; When I try to instantiate B,…
Loom
  • 9,768
  • 22
  • 60
  • 112
7
votes
1 answer

CRTP + Traits class : "no type named..."

I try to implement a CRTP with templated class and I have an error with the following example code : #include template class Traits { public: typedef typename T::type type; // <- Error …
Vincent
  • 57,703
  • 61
  • 205
  • 388
7
votes
6 answers

Can I use the Curiously Recurring Template Pattern here (C++)?

I have a C++ application that can be simplified to something like this: class AbstractWidget { public: virtual ~AbstractWidget() {} virtual void foo() {} virtual void bar() {} // (other virtual methods) }; class WidgetCollection { …
David
6
votes
5 answers

C# - Intrusive tree structure, using CRTP

I'm currently working on a simple way to implement a intrusive tree structure in C#. As I'm mainly a C++ programmer, I immediatly wanted to use CRTP. Here is my code: public class TreeNode where T : TreeNode { public void AddChild(T…
s0ubap
  • 267
  • 3
  • 8
6
votes
1 answer

crtp and type visibility

I have this puzzle which I am trying to solve, and fundamentally it boils down to the following example: template struct A { typedef typename CT::VALUE_T FOO; // FOO is dependent on CT }; template struct B { typedef…
Nim
  • 33,299
  • 2
  • 62
  • 101
6
votes
1 answer

CRTP operator= and Concepts

Clang rejects this demo, while GCC and MSVC accept it. (https://godbolt.org/z/M1Wsxs8fj) Who is correct? Or is this ill-formed no diagnosis required? #include #if 0 #define METHOD balabala // OK #else #define METHOD operator= //…
VainMan
  • 2,025
  • 8
  • 23
6
votes
2 answers

Three-way comparison of CRTP-ed std::vectors

In this article I've stumbled upon this obscure code (which is presented casually, like this is a totally normal C++ code): struct Tree : std::vector {}; Two trees are then created and compared (see the demo): Tree tree(size_t h) { Tree s; …
Mikhail
  • 20,685
  • 7
  • 70
  • 146
6
votes
1 answer

Access to own private constructors via derived class constructor inheritance

Consider: struct B { void f(); private: B(int, int = 0); }; struct D : B { using B::B; }; void B::f() { auto a = D{0}; auto b = D(0); auto c = D(0, 0); D x{0}; D y(0); D z(0, 0); } GCC accepts (since 7.1; previously…
ecatmur
  • 152,476
  • 27
  • 293
  • 366