crtp

Policy class design but without making the whole user class a template

試著忘記壹切 提交于 2021-01-19 06:28:33
问题 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 are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Variadic CRTP Base Class with Additional Template Parameters

余生长醉 提交于 2021-01-07 02:19:19
问题 I have a trait classes which are about to be used in variadic CRTP to extend the features of SmartPointer class. This question is created as the followup of https://stackoverflow.com/a/65373058/5677080 An example trait class: template<typename DERIVED, typename DELETER> class Owning { public: using deleter_type = DELETER; /* ... */ }; Here comes the implemnetation of my SmartPointer which is being extended by variadic CRTP by those trait classes: template<typename T, template<typename> class

Compilation issues with smart pointer-based CRTP idiom

亡梦爱人 提交于 2020-06-12 07:24:10
问题 I'm trying to compile a minimal working example for the CRTP example given in this blog post, which is based on smart pointers. Based on the code example, I've written two files, a header and source. Header ( crtp.h ): #include <memory> class Cloneable { public: virtual ~Cloneable() {} std::shared_ptr<Cloneable> clone() const { return std::shared_ptr<Cloneable>(this->clone_raw()); } private: virtual Cloneable* clone_raw() const = 0; }; template <typename Derived, typename Base> class

C++ CRTP initialization

我是研究僧i 提交于 2020-05-26 08:22:21
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault

C++ vector of CRTP shared pointers

拜拜、爱过 提交于 2020-05-16 06:01:05
问题 In my search for a way to store CRTP objects in a container, I found the following question: A polymorphic collection of Curiously Recurring Template Pattern (CRTP) in C++? I tryied the marked solution https://stackoverflow.com/a/24795227/5475431 but the compiler is complainings erros like: no known conversion for argument 1 from ‘std::shared_ptr<DerivedA>’ to ‘const std::shared_ptr<BaseInterface>&’ here is my try: #include <vector> #include <memory> struct BaseInterface { virtual

Confusion about CRTP static polymorphism

那年仲夏 提交于 2020-05-10 04:26:22
问题 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 <class T> struct Base { void implementation() { // ... static_cast<T*>(this)->implementation(); // ... } static void static_func() { // ... T::static_sub_func(); // ... } }; struct Derived : public Base<Derived> { void implementation(); static void static_sub_func(); }

Confusion about CRTP static polymorphism

不羁岁月 提交于 2020-05-10 04:25:07
问题 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 <class T> struct Base { void implementation() { // ... static_cast<T*>(this)->implementation(); // ... } static void static_func() { // ... T::static_sub_func(); // ... } }; struct Derived : public Base<Derived> { void implementation(); static void static_sub_func(); }

C++ CRTP initialization

那年仲夏 提交于 2020-04-28 20:52:59
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault