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

multiple curiously recurring template pattern (CRTP) in c#?

Im trying to implement CRTP interface to my code, but the constraint make me stuck. how to implement the constraints if i have code structure look like this? Is this legal? Thank you. interface IInterface where T: IInterface { //bla…
user2277061
  • 7
  • 1
  • 4
0
votes
0 answers

CRTP and the assignment operator

I have some code that assigned to a CRTP class like this: IChunkWindow> editWindow = chunkWindow.create( chunkWindow.getOrigin().getX() + x /…
Xavier
  • 7,987
  • 13
  • 61
  • 93
0
votes
2 answers

Can a class inherit from both an abstract class and a CRTP class?

Can a class inherit from both an abstract class and a CRTP class? Or if I inherit from a CRTP class must all classes I inherit from use CRTP?
Xavier
  • 7,987
  • 13
  • 61
  • 93
0
votes
2 answers

How to handle CRTP in a class hierarchy?

In one of my projects I'm using the same CRTP approach (deriving from enable_crtp) as in Answer 1 here: How do I pass template parameters to a CRTP? However I have the need to derive from the derived classes too. Is there any way to make this work…
fhw72
  • 798
  • 1
  • 8
  • 19
0
votes
0 answers

improving crtp error message when using same function name in base and derived classes

When using crtp is there a nice way to improve the error message (maybe using static_assert in some way) that the compiler generates when you have a member function with the same name in both the base and derived classes? Here is an example of what…
user1594173
  • 103
  • 5
0
votes
1 answer

Boost iterator_facade and forward declarations

I want to create a custom container that supports iterators. It looks like this: class SomeContainer { ... public: typedef SomeIterator iterator; iterator begin() { ... } iterator end() { ... } }; Then I create an iterator for…
petersohn
  • 10,129
  • 11
  • 54
  • 87
0
votes
2 answers

allocating memory for derived class members based on boost smart pointers in the base class through CRTP

This part of the question provides background information and can be ignored I am working on a template library which heavily relies on the use of the curiously recurring template pattern. The idea behind the class structure is that the user can…
user1391279
0
votes
2 answers

C++ : CRTP destructor?

On a project, I have the following problem : I have a very simple inheritance scheme (I need inheritance and not composition) : class Base -> class DerivedA -> class DerivedB -> class DerivedC A, B and C derive from Base and that's all. So now I…
Vincent
  • 50,257
  • 51
  • 171
  • 339
0
votes
2 answers

CRTP intermediate class that needs to also be made final

I have an inheritance chain of CRTP classes. The CRTP classes derive from each other, until a 'final' derived class passes itself as the CRTP parameter and finalizes the inheritance chain. template struct Base { ..... }; template…
Alexander Vassilev
  • 1,310
  • 13
  • 20
-1
votes
2 answers

curiously recurring template pattern and infinite recursion

Take a look at the following simple curiously recurring template pattern (CRTP) example: template struct base { void foo() { static_cast(this)->foo(); } }; struct derived : public…
0xbadf00d
  • 14,584
  • 15
  • 60
  • 93
-1
votes
1 answer

Calling an overloaded parent's method from a child class in C++

I'm trying to understand if is possible to call a parent's function member from a child class. Basically I have the following code: struct Parent { template void doFoo(Args&&... args) { std::cout << "parent doFoo"; …
Saturnu
  • 309
  • 1
  • 8
-1
votes
2 answers

Corrupt member variable in derived class with Curiously Recurring Templating Pattern

I'm currently playing around with CRTP and am coming across the issue of a member variable in the derived class being corrupted aka having a garbage value (there are currently 4 levels of polymorphism, with the top most base class called "A" and the…
lockdown
  • 3
  • 2
-1
votes
1 answer

CRTP Calling Child Virtual Override in Base Constructor

The following example produces this run-time error: pure virtual method called terminate called without an active exception e.g. template struct Base { int val; Base(int i) : val(static_cast(this)->init_val(i)) {} …
user9816683
  • 107
  • 4
-1
votes
1 answer

C2131 Error for static_assert () in C++

Recently I was trying to create a class that can have only one instantiated object. On searching Wikipedia I found that using the singleton pattern would help out. I didn't like the idea of it and tried to do something of my own. Here is the code…
-1
votes
1 answer

CRTP why is member lookup runtime?

Or at least I think it is. Consider the following code #include #include struct BaseBase { virtual void foo() = 0; virtual ~BaseBase(){} }; template struct Base : BaseBase{ void foo() override{ …
arynaq
  • 6,237
  • 9
  • 34
  • 70
1 2 3
44
45