crtp

mixing CRTP with SFINAE

喜你入骨 提交于 2019-12-03 21:46:17
I have a base taking derived type as template parameter. The following code works as expected. instantiation of base<non_default_impl> uses non_default_impl::data_t and base<default_impl> throws compilation error because event_data is only a forward declaration. template <typename T> struct event_data; template<typename T> struct tovoid { typedef void type; }; template <typename T, typename enable = void> struct get_data{ typedef event_data<T> type; }; template <typename T> struct get_data<T, typename tovoid<typename T::data_t>::type >{ typedef typename T::data_t type; }; template <typename T>

CRTP to avoid virtual member function overhead

本小妞迷上赌 提交于 2019-12-03 21:11:08
In CRTP to avoid dynamic polymorphism , the following solution is proposed to avoid the overhead of virtual member functions and impose a specific interface: template <class Derived> struct base { void foo() { static_cast<Derived *>(this)->foo(); }; }; struct my_type : base<my_type> { void foo() {}; // required to compile. < Don't see why }; struct your_type : base<your_type> { void foo() {}; // required to compile. < Don't see why }; However it seems that the derived class does not require a definition to compile as it inherits one (the code compiles fine without defining a my_type::foo). In

Mixins, variadic templates, and CRTP in C++

╄→尐↘猪︶ㄣ 提交于 2019-12-03 17:02:57
问题 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 to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4

Curiously recurring template - variation

自古美人都是妖i 提交于 2019-12-03 12:57:25
问题 Regarding CRP if I want to implement a slight variation of it (using template template parameter) I get a compile error: template <template <typename T> class Derived> class Base { public: void CallDerived() { Derived* pT = static_cast<Derived*> (this); pT->Action(); // instantiation invocation error here } }; template<typename T> class Derived: public Base<Derived> { public: void Action() { } }; I am not exactly sure one would chose this form (that does not compile for me) instead of using

CRTP and c++1y return type deduction

感情迁移 提交于 2019-12-03 11:37:35
I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned, but if I modify the functio foo like this: auto foo() -> decltype(static_cast<Derived*>(this)->foo_impl(

Mixins, variadic templates, and CRTP in C++

我们两清 提交于 2019-12-03 06:00:28
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 to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4.4.1: template <template<class> class... Mixins> class Host : public Mixins<Host<Mixins>>... { public:

Curiously recurring template - variation

走远了吗. 提交于 2019-12-03 03:13:38
Regarding CRP if I want to implement a slight variation of it (using template template parameter) I get a compile error: template <template <typename T> class Derived> class Base { public: void CallDerived() { Derived* pT = static_cast<Derived*> (this); pT->Action(); // instantiation invocation error here } }; template<typename T> class Derived: public Base<Derived> { public: void Action() { } }; I am not exactly sure one would chose this form (that does not compile for me) instead of using this though (this works) template <typename Derived> class Base { public: void CallDerived() { Derived*

C++ CRTP and accessing derived's nested typedefs from base

左心房为你撑大大i 提交于 2019-12-03 01:51:28
edit: I'll put a github link here when I am done altering my design for anyone who is interested. Background I'm replacing a boost::intrusive , intrusive_set , with my own implementation as 64-bit compiled intrusive-set stuffs 3 x 8-byte pointers into my container nodes. my container has a limit of 2^16 nodes so I can bring it down to 4-bytes per node with 2x 16-bit offset ordinals (which is a 6x reduction of size). In the example below base is the intrusive-set container. The derived class has a std::vector<container_entry_type<entry_type> > . obviously with this level of indirection I need

Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

百般思念 提交于 2019-12-03 00:42:03
One of the main benefits of virtual in C++ is being able to use the base class (pointer or reference) to call derived methods. I'm reading up on using CRTP to implement static polymorphism , but I can't understand how to achieve what I've mentioned above using this technique, because I can't declare a function as taking type Base when this requires a template. It seems to me that what is described in the article could be achieved by simply using function overloading, so I'm sure that there must be more to this technique. (PS: this exact problem is alluded to in a comment to an answer to this

How to write curiously recurring templates with more than 2 layers of inheritance?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 18:12:45
All the material I've read on Curiously Recurring Template Pattern seems to one layer of inheritance, ie Base and Derived : Base<Derived> . What if I want to take it one step further? #include <iostream> using std::cout; template<typename LowestDerivedClass> class A { public: LowestDerivedClass& get() { return *static_cast<LowestDerivedClass*>(this); } void print() { cout << "A\n"; } }; template<typename LowestDerivedClass> class B : public A<LowestDerivedClass> { public: void print() { cout << "B\n"; } }; class C : public B<C> { public: void print() { cout << "C\n"; } }; int main() { C c; c