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

CRTP with Template Template Arguments

The following code doesn't compile... namespace { template class D> struct Base { Base(const T& _t) : t(_t) { } T t; }; template struct Derived : Base { …
19
votes
1 answer

clang++ not accepting use of template template parameter when using CRTP

I'm getting compilation errors when trying to call the base class constructor in derived initialization list when using a template template parameter with CRTP. Problem can be replicated with this snippet of code: template class…
goji
  • 6,384
  • 3
  • 34
  • 56
18
votes
5 answers

Is it possible to access child types in c++ using CRTP?

I have this toy example, template struct Base { template using Foo = typename TChild::template B; }; struct Child : Base { template using B = T; }; using Bar =…
bradgonesurfing
  • 28,325
  • 12
  • 101
  • 188
16
votes
3 answers

C++ CRTP and accessing derived's nested typedefs from base

edit: I'll put a github link here when I am done altering my design for anyone who is interested. Background I'm replacing a boost::intrusive, intrusive_set, with my own implementation as 64-bit compiled intrusive-set stuffs 3 x 8-byte pointers into…
Hassan Syed
  • 19,054
  • 9
  • 76
  • 156
16
votes
1 answer

Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived class

Consider the following code: template struct S { static constexpr int bar = T::foo; }; struct U: S { static constexpr int foo = 42; }; int main() { } GCC v6.1 compiles it, clang 3.8 rejects it with the error: 2 : error: no member…
skypjack
  • 45,296
  • 16
  • 80
  • 161
15
votes
5 answers

How to avoid errors while using CRTP?

Using CRTP sometimes I write a code like this: // this was written first struct Foo : Base { ... }; // this was copy-pasted from Foo some days later struct Bar : Base { ... }; And it's very difficult to understand what…
Abyx
  • 10,859
  • 4
  • 36
  • 74
15
votes
2 answers

CRTP and c++1y return type deduction

I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template struct Base { auto foo() { return…
Morwenn
  • 19,202
  • 10
  • 89
  • 142
15
votes
1 answer

Using declaration for type-dependent template name

When CRTP is used inside a template, (or generally when a template parameter is passed as a base class template argument), is it impossible to name the base's member templates in a using declaration? template< typename d > struct base { …
Potatoswatter
  • 126,977
  • 21
  • 238
  • 404
14
votes
3 answers

Confusion about CRTP static polymorphism

I'm trying to wrap my head around the CRTP. There are some good sources around, including this forum, but I think I have some confusion about the basics of static polymorphism. Looking at the following Wikipedia entry: template struct…
user32849
  • 549
  • 4
  • 13
13
votes
2 answers

Two different mixin patterns in C++. (mixin? CRTP?)

I'm studying about mixins (in C++). I read some articles on mixins and found two different patterns of "approximating" mixins in C++. Pattern 1: template struct Mixin1 : public Base { }; template struct Mixin2 : public Base…
upxuk
  • 133
  • 4
12
votes
1 answer

Downcast to derived class in CRTP base class constructor: UB or not?

Consider the following classes: template class BaseCRTP { private: friend class LinkedList; Derived *next = nullptr; public: static LinkedList instances; BaseCRTP() { …
tttapa
  • 1,199
  • 7
  • 21
12
votes
1 answer

std::declval vs crtp, cannot deduce method return type from incomplete type

I am trying to do something like this (in c++11): #include template struct base { using type = decltype( std::declval().foo() ); }; struct bar : base { int foo() { return 42;} }; int main() { bar::type…
463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
12
votes
1 answer

Why has this C++ code an ambiguous method call only on Microsoft compiler?

I'm trying to compile a library on microsoft C++ compiler 14.1 (Visual Studio 2017) but I'm getting a strange error due to an ambiguous call to a class method. After some testing I isolated the following code snippet: #include struct…
12
votes
2 answers

C++ CRTP virtual function point of instantiation

I'm trying to understand if a simple CRTP pattern is valid by the standard. The code below compiles and works as expected (on clang). But my understanding of the relevant standard chapters/paragraphs is that the point of instantiation of the…
Francesco
  • 215
  • 1
  • 7
12
votes
3 answers

Possibility to mix composite pattern and curiously recurring template pattern

I have a composite pattern implementation, used for GUI components: class CObject { private: CObject * m_pParent; CObjectContainer * m_pChildren; void private_foo() { this->foo(); //Calls private_foo for each child in container. …
1
2
3
44 45