crtp Questions

4

Solved

After reading this answer I have tried implementing some simple CRTP usage. I figured I'd try to implement the Singleton (yes, I know - it's just for practice and research) pattern, given the fact ...
Megillah asked 22/8, 2018 at 20:31

3

Solved

For context: I'm constructing a CRTP Mixin for my derived class. Now I want to call all protected base class member functions if they exist. I want to have the base class member functions not expos...
Orgy asked 8/3 at 7:39

2

Short Version of Question C++23 gives us a new way to write mixin classes (instead of CRTP). Is there any context where CRTP would still be preferred? Summary of the Two Approaches CRTP is a powerf...
Sandstone asked 26/10, 2023 at 23:33

1

Solved

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 <type_traits> #if 0 #define MET...
Aminopyrine asked 21/3, 2023 at 16:45

7

A friend of mine asked me "how to use CRTP to replace polymorphism in a multilevel inheritance". More precisely, in a situation like this: struct A { void bar() { // do something and then call ...
Nacred asked 11/8, 2013 at 17:2

3

Solved

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 pat...
Renie asked 23/6, 2009 at 14:39

1

Solved

I have a very simple CRTP skeleton structure that contains just one vector and a private accessor in the base class. There is a helper method in the CRTP class to access it. #include <vector>...
Schwaben asked 10/6, 2022 at 21:5

6

Solved

Without referring to a book, can anyone please provide a good explanation for CRTP (curiously recurring template pattern) with a code example?
Suilmann asked 13/11, 2010 at 15:30

2

Solved

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 ...
Radical asked 7/6, 2012 at 21:9

1

Solved

In CRTP, the base class can use functions and variables from derived class. However, the types from derived class cannot be used directly by base class, see code below: #include <iostream> t...
Paperback asked 25/8, 2021 at 10:31

2

Solved

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<Tree> {}; Two trees are then created and...
Cadmann asked 22/3, 2021 at 10:0

5

Solved

I have this toy example, template <typename TChild> struct Base { template <typename T> using Foo = typename TChild::template B<T>; }; struct Child : Base<Child> { templ...
Droopy asked 17/3, 2021 at 7:21

1

Solved

Is something like this valid C++20 code? #include <iostream> template <typename T> concept impls_decrement = requires(T it) { it.decrement(); }; template <class Derived> struct ...
Faizabad asked 16/2, 2021 at 1:8

5

How can I use CRTP in C++ to avoid the overhead of virtual member functions?
Hyaloplasm asked 4/11, 2008 at 16:7

2

Solved

Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams ar...
Hurley asked 8/1, 2021 at 11:29

1

Solved

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 acc...
Dirham asked 16/12, 2020 at 9:9

1

Solved

In CRTP, the base object can return a reference to the derived object via static cast. Is this also true in the case of multiple inheritance? The second base and beyond might be at addresses differ...

1

Consider the following classes: template <class Derived> class BaseCRTP { private: friend class LinkedList<Derived>; Derived *next = nullptr; public: static LinkedList<D...
Steinke asked 6/4, 2020 at 13:32

1

Solved

template<template<typename, size_t>class V, typename, size_t N> struct X{ static constexpr size_t stride = N; }; template<typename Num, size_t N> struct Y; template<typename ...
Wawro asked 20/10, 2020 at 10:27

1

Consider this: template<typename T> struct base_t { auto& f(int x) { return (T&)*this; } auto& f(char x) { return (T&)*this; } }; struct derived_t : base_t<derived_t>...
Deterrent asked 15/8, 2020 at 19:31

2

Solved

I have a use of the CRTP that doesn't compile with g++ 4.2.1, perhaps because the derived class is itself a template? Does anyone know why this doesn't work or, better yet, how to make it work? Sam...
Leesaleese asked 30/5, 2010 at 21:25

1

Solved

The CRTP pattern allows to emulate the so called self types in Java, e. g.: abstract class AbstractFoo<SELF extends AbstractFoo<SELF>> implements Comparable<SELF> { @Overr...
Sublimation asked 2/10, 2018 at 15:4

7

Solved

I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum: class Enum<E extends Enum<E>> Could someone explain how to interpret this ty...
Paediatrics asked 17/10, 2008 at 5:14

1

Solved

I am new to the concept of a 'Curiously Recurring Template Pattern', and I'm reading about its potential use cases here. In that article, the author describes a simple case where we have two or mo...
Pollen asked 16/12, 2019 at 0:42

3

In the CRTP pattern, we run into problems if we want to keep the implementation function in the derived class as protected. We must either declare the base class as a friend of the derived class or...
Catherine asked 15/12, 2011 at 16:57

© 2022 - 2024 — McMap. All rights reserved.