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
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

Why does this code give the error, "template specialization requires 'template<>'"?

When I try to compile this with Clang template struct Field { char const *name; Field(char const *name) : name(name) { } }; template class CRTP { static Field const _field; }; class Class : public…
user541686
  • 205,094
  • 128
  • 528
  • 886
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. …
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
  • 256,549
  • 94
  • 388
  • 662
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
  • 949
  • 1
  • 6
  • 21
11
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…
djsp
  • 2,174
  • 2
  • 19
  • 40
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
  • 1
  • 7
  • 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
  • 12,242
  • 10
  • 53
  • 87
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
  • 57,703
  • 61
  • 205
  • 388
10
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.
  • 3,461
  • 1
  • 21
  • 41
10
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…
10
votes
1 answer

CRTP -- accessing incomplete type members

Related questions: one, two After trying to understand CRTP for several days it seems that now I understand even less than before:) Consider the following code: #include template class Interace { public: typedef typename…
DimG
  • 1,641
  • 1
  • 16
  • 23
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
  • 3,157
  • 2
  • 21
  • 46
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
  • 1
  • 7
10
votes
2 answers

What is the difference between Strategy and CRTP for static polymorphism?

I want to have an interface with multiple possible implementations, selected at compile-time. I saw that CRTP is the idiom of choice for implementing this. Why is that? An alternative is the Strategy pattern, but I see no mention of this technique…
Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
1 2
3
50 51