crtp

Smart pointer toolkit using variadic CRTP

只愿长相守 提交于 2021-02-11 12:52:28
问题 I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there. Design Considerations: I wanna use variadic CRTP approach to mixin the

Inheriting from a template class set of operators using CRTP

社会主义新天地 提交于 2021-02-11 12:40:35
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Inheriting from a template class set of operators using CRTP

家住魔仙堡 提交于 2021-02-11 12:39:12
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

C++ CRTP class hierarchy

て烟熏妆下的殇ゞ 提交于 2021-02-07 03:50:14
问题 From Wikipedia: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; struct derived : base<derived> { // ... }; Now if I want derived_from_derived , I can write: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; template <typename T> struct derived : base<T> { // ... }; struct derived_from_derived : derived <derived_from_derived> { // ... }; Now suppose I just want a derived object. This doesn't work:

C++ CRTP class hierarchy

半世苍凉 提交于 2021-02-07 03:42:56
问题 From Wikipedia: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; struct derived : base<derived> { // ... }; Now if I want derived_from_derived , I can write: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; template <typename T> struct derived : base<T> { // ... }; struct derived_from_derived : derived <derived_from_derived> { // ... }; Now suppose I just want a derived object. This doesn't work:

Using inner class with CRTP

本小妞迷上赌 提交于 2021-02-04 17:54:05
问题 Is there any possibility to use inner class or enum with CRTP? Ex. template<typename Container> struct ContainerBase { std::map<typename Container::Enum, int> _; }; struct ConcreteContainer : ContainerBase<ConcreteContainer> { enum class Enum { left, right }; }; 回答1: No. Within the class template the derived class isn't fully defined yet (that's not a complete object in standardese ). In fact (working draft): A class is considered a completely-defined object type (or complete type) at the

Iterate over class inheritances in C++

元气小坏坏 提交于 2021-01-28 14:02:08
问题 Assume I have a some classes architecture (the number of the classes is growing up during the development time), that each class inherit from N classes with the same basic interface. What is the best way (if possible) to create a base function (in the base class OR in the derived class) that will iterate over the inheritances? Target: Avoid developers mistakes and make sure we won't forget to call all the base functions from all of the inheritances & make the code more clear to read and

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

痞子三分冷 提交于 2021-01-19 06:35:32
问题 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 +

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

ぃ、小莉子 提交于 2021-01-19 06:32:52
问题 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 +

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

梦想的初衷 提交于 2021-01-19 06:32:02
问题 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 +