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

How to write a good curiously recurring template pattern (CRTP) in C#

A while back I wanted to create my own data mapper that would be much simpler than your average ORM. In doing so I found the need to have access to the type information of inheriting classes in my base class. My first thought was reflection, but…
Brandon Moore
  • 8,110
  • 15
  • 59
  • 115
11
votes
6 answers

reusing the copy-and-swap idiom

I'm trying to put the copy-and-swap idiom into a reusable mixin: template struct copy_and_swap { Derived& operator=(Derived copy) { Derived* derived = static_cast(this); derived->swap(copy); …
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
11
votes
3 answers

Use Curiously Recurring Template Pattern (CRTP) with additional type parameters

I try to use the Curiously Recurring Template Pattern (CRTP) and provide additional type parameters: template class Base { Int *i; Float *f; }; ... class A : public Base
hrr
  • 1,687
  • 2
  • 17
  • 35
11
votes
3 answers

How to implement the CRTP following MISRA C++

My team is developing a embedded system where we need to follow MISRA C++. We are refactoring the code to use less virtual methods so we are trying to implement the CRTP to use static polymorphism instead of the dynamic one. But we have the problem…
LeDYoM
  • 895
  • 6
  • 18
11
votes
1 answer

Mixins, variadic templates, and CRTP in C++

Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144). However, I'd also like the mixins…
Eitan
  • 862
  • 6
  • 17
11
votes
1 answer

Inferring return type of templated member functions in CRTP

Is it possible to infer the return type of a templated member function in a CRTP base class? While inferring argument types works well, it fails with the return type. Consider the example below. #include template
mavam
  • 11,144
  • 9
  • 46
  • 83
11
votes
2 answers

Clang and Intel fail to compile this CRTP code

I have written a small library that use a lot of C++11 metaprogramming techniques and CRTP, and it compiles well with g++ 4.7.2 Now, I try to compile it with Intel icpc 13.0.0.079 and it generates several hundreds of errors. So I try to isolate the…
Vincent
  • 50,257
  • 51
  • 171
  • 339
10
votes
1 answer

Passing overloaded CRTP class member method to lambda

Consider this: template struct base_t { auto& f(int x) { return (T&)*this; } auto& f(char x) { return (T&)*this; } }; struct derived_t : base_t { }; void test() { derived_t instance; auto lambda =…
10
votes
3 answers

Ensure that class derived from parent CRTP class implements function

Brief: I want to make sure a derived class implements a member function required by a function within the parent CRTP class. Detail: I have some code like this class Base { public: class Params { public: virtual ~Params() {} …
user2746401
  • 2,497
  • 1
  • 18
  • 41
10
votes
3 answers

Template definition of non-template error

I want to use the CRTP pattern in combination with some locking mechanism for access syncing in multithreaded environment. My code looks like this: //-- CRTP base class with some sync/lock mechanism template struct Base { …
Martin Pasko
  • 101
  • 7
10
votes
2 answers

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is…
Doug
  • 8,190
  • 1
  • 25
  • 37
9
votes
3 answers

How to fix a purported lack of an "explicit instantiation declaration" when compiling a CRTP Singleton with Clang?

We're using the curiously recurring template pattern to implement singletons. However, with recent Clang versions we're getting a -Wundefined-var-template warning. The suggested fix to which is to add an "explicit instantiation declaration". I…
R.M.
  • 2,938
  • 1
  • 17
  • 33
9
votes
3 answers

CRTP - Checking from the base class that the derived one meets requirements

The curiously recurring template pattern may be used to implement a sort of static polymorphism. For example: #include template< class Derived > struct Base { static void print ( ) { std::cout << Derived::number_to_print…
Kalrish
  • 1,938
  • 2
  • 16
  • 37
9
votes
2 answers

Curiously recurring template pattern (CRTP) with static constexpr in Clang

Consider my simple example below: #include template class Base { public: static constexpr int y = T::x; }; class Derived : public Base { public: static constexpr int x = 5; }; int main() { std::cout…
9
votes
1 answer

C++ Low latency Design: Function Dispatch v/s CRTP for Factory implementation

As part of a system design, we need to implement a factory pattern. In combination with the Factory pattern, we are also using CRTP, to provide a base set of functionality which can then be customized by the Derived classes. Sample code below:…
Sid
  • 119
  • 7
1 2
3
44 45