crtp

Derived curiously recurring templates and covariance

独自空忆成欢 提交于 2019-11-30 03:25:01
问题 Suppose I have a base class which cloning of derived classes: class Base { public: virtual Base * clone() { return new Base(); } // ... }; I have a set of derived classes which are implemented using a curiously recurring template pattern: template <class T> class CRTP : public Base { public: virtual T * clone() { return new T(); } // ... }; And I attempt to derive from that further like this: class Derived : public CRTP<Derived> { public: // ... }; I get compilation errors to the effect of:

How to partially specialize a class template for all derived types?

a 夏天 提交于 2019-11-30 03:12:02
问题 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 pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base class, then it's easy, I can just write: namespace std { namespace tr1 { template <typename Derived> struct hash<CRTPBase<Derived> > { size_t operator()

Protect CRTP pattern from stack overflowing in “pure virtual” calls

末鹿安然 提交于 2019-11-30 00:31:43
问题 Consider the following standard CRTP example: #include <iostream> template<class Derived> struct Base { void f() { static_cast<Derived *>(this)->f(); } void g() { static_cast<Derived *>(this)->g(); } }; struct Foo : public Base<Foo> { void f() { std::cout << 42 << std::endl; } }; int main() { Foo foo; foo.f(); // just OK foo.g(); // this will stack overflow and segfault } If this was regular virtual inheritance I could have mark virtual f and g methods as pure like struct Base { virtual void

Inferring return type of templated member functions in CRTP

断了今生、忘了曾经 提交于 2019-11-29 18:21:33
问题 Is it possible to infer the return type of a templated member function in a CRTP base class? While inferring argument types works well, it fails with the return type. Consider the example below. #include <iostream> template <typename Derived> struct base { template <typename R, typename T> R f(T x) { return static_cast<Derived&>(*this).f_impl(x); } }; struct derived : base<derived> { bool f_impl(int x) { std::cout << "f(" << x << ")" << std::endl; return true; } }; int main() { bool b =

Inheritance in curiously recurring template pattern polymorphic copy (C++)

人盡茶涼 提交于 2019-11-29 11:18:46
I'm using CRTP to add a clone method to inherited classes, for example: class Base { virtual ~Base() {}; virtual Base* clone() const = 0; }; template<class Derived> class BaseCopyable : Base { public: virtual Base* clone() const { return new Derived(static_cast<Derived const&>(*this)); } }; class A : public BaseCopyable<A>; class B : public BaseCopyable<B>; etc... But if I have a class that inherits from B, for example: class differentB : public B; Then clone() doesn't return an object of type differentB, it returns a B. Besides writing a new clone() method in differentB, is there some way to

How do I pass template parameters to a CRTP?

六月ゝ 毕业季﹏ 提交于 2019-11-29 08:04:41
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 TemplateRex The CRTP pattern is typically used to enable static polymorphism and the ability to mixin (parametrized) behavior. To illustrate two alternatives, it's convenient to first define a

Ensure derived class implements static method

百般思念 提交于 2019-11-29 07:06:11
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 : public Base <Derived> { public: static int foo(int i) { return 42; }; }; However, it does not compile

Prevent user from deriving from incorrect CRTP base

混江龙づ霸主 提交于 2019-11-28 19:16:26
I cannot think about a proper question title to describe the problem. Hopefully the details below explains my problem clear. Consider the following code #include <iostream> template <typename Derived> class Base { public : void call () { static_cast<Derived *>(this)->call_impl(); } }; class D1 : public Base<D1> { public : void call_impl () { data_ = 100; std::cout << data_ << std::endl; } private : int data_; }; class D2 : public Base<D1> // This is wrong by intension { public : void call_impl () { std::cout << data_ << std::endl; } private : int data_; }; int main () { D2 d2; d2.call_impl();

CRTP and multilevel inheritance

元气小坏坏 提交于 2019-11-28 15:53:08
问题 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 foo (possibly) in the derived class: foo(); } // possibly non pure virtual virtual void foo() const = 0; } struct B : A { void foo() const override { /* do something */ } } struct C : B { // possibly absent to not override B::foo(). void foo() const final { /* do something else */ } } My friend and I

What would a CRTP-based solution to this look like?

让人想犯罪 __ 提交于 2019-11-28 10:45:51
问题 I asked the following question in this post (pasted below for convenience). One of the comments suggested that there is a CRTP-based solution to the problem. I am not able to figure out how CRTP is relevant here (well, I never used CRTP before, so I am not used to thinking in these terms). So, how would a CRTP-based solution look like? Here is the cited question: Is it possible to write a template function that would possess type information about the base class of the template argument?