crtp

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

Deadly 提交于 2019-12-20 10:54:22
问题 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

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

非 Y 不嫁゛ 提交于 2019-12-20 09:16:17
问题 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

CRTP with Protected Derived Member

陌路散爱 提交于 2019-12-20 08:42:18
问题 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 use something like this (I have not tried the method on the linked article). Is there some other (simple) way that allows keeping the implementation function in the derived class as protected? Edit: Here is a simple code example: template<class D> class C { public: void base_foo() { static_cast<D*>

CRTP with Protected Derived Member

最后都变了- 提交于 2019-12-20 08:42:00
问题 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 use something like this (I have not tried the method on the linked article). Is there some other (simple) way that allows keeping the implementation function in the derived class as protected? Edit: Here is a simple code example: template<class D> class C { public: void base_foo() { static_cast<D*>

CRTP compiling error

馋奶兔 提交于 2019-12-19 08:43:44
问题 The following will compile with GCC 5.2 but not with Visual Studio 2015. template <typename Derived> struct CRTP { static constexpr int num = Derived::value + 1; }; struct A : CRTP<A> { static constexpr int value = 5; }; It complains that A does not have a member named value . How to fix the code so that it compiles on both compilers? Or is it illegal altogether? 回答1: Try making it a constexpr function instead. The way you have it setup now attempts to access an incomplete type. Since a

C++ CRTP virtual function point of instantiation

馋奶兔 提交于 2019-12-19 05:18:10
问题 I'm trying to understand if a simple CRTP pattern is valid by the standard. The code below compiles and works as expected (on clang). But my understanding of the relevant standard chapters/paragraphs is that the point of instantiation of the virtual function CRTP< Derived, Base >::DoSomething() should be at point (B) of the code, where the full declaration of Derived is not available. Therefore the inner typedef Type should not be available either. Can anyone kindly point out the relevant

Invalid covariant type with CRTP clonable class

只愿长相守 提交于 2019-12-18 08:29:24
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

Invalid covariant type with CRTP clonable class

泄露秘密 提交于 2019-12-18 08:28:18
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

How do I pass template parameters to a CRTP?

落花浮王杯 提交于 2019-12-18 05:11:52
问题 In the following code: template <typename T> class CRTP { public: }; template <int I, typename T> class CRTPInt { public: }; template <template <typename> class T> class Derived : public T<Derived<T>> { public: }; void main() { Derived<CRTP> foo; Derived<CRTPInt<2>> foo2; } How do I write CRPTInt so I can pass in a templatized parameter that will then be continued in the Derived definition? Thanks, Jim 回答1: The CRTP pattern is typically used to enable static polymorphism and the ability to

Ensure derived class implements static method

青春壹個敷衍的年華 提交于 2019-12-18 04:52:08
问题 I want to ensure, that a derived class implements a specific static method. I think doing so should be possible using static_assert, std::is_same, decltype, CRTP and maybe making use of SFINAE. However, similar code I found so far is quite complex and it seems I do not yet fully understand it making me unable to adopt it to my needs. What I tried so far is this template <class T> class Base { static_assert(std::is_same<decltype(T::foo(1)), int>::value, "ERROR STRING"); }; class Derived :