crtp

C++ CRTP initialization

大憨熊 提交于 2020-04-28 20:44:19
问题 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++ CRTP initialization

╄→尐↘猪︶ㄣ 提交于 2020-04-28 20:43:39
问题 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

Ambiguous method from inheritance when using a CRTP pattern

試著忘記壹切 提交于 2020-01-23 12:18:26
问题 I am defining a DoubleWrapper class inheriting from two CRTP base classes, Ratioable and Divable , that both define operator/() , with different signatures: T operator/(double const& scalar) const { return T(this->underlying().get() / scalar); } double operator/(T const& other) const { return this->underlying().get() / other.get(); } They differ both by return type and parameter type. However compiler is complaining about operator/() being ambiguous. Note that the constructor is explicit so

Ambiguous method from inheritance when using a CRTP pattern

我怕爱的太早我们不能终老 提交于 2020-01-23 12:18:18
问题 I am defining a DoubleWrapper class inheriting from two CRTP base classes, Ratioable and Divable , that both define operator/() , with different signatures: T operator/(double const& scalar) const { return T(this->underlying().get() / scalar); } double operator/(T const& other) const { return this->underlying().get() / other.get(); } They differ both by return type and parameter type. However compiler is complaining about operator/() being ambiguous. Note that the constructor is explicit so

CRTP: why a difference between getting a nested type and nested method of the derived class?

我的梦境 提交于 2020-01-14 08:00:10
问题 The base class in the CRTP pattern can access the member functions of the derived class, but it can't access a nested type in the derived class. Why this difference? To illustrate, consider the following piece of code: template<typename Derived> struct crtp_base { void crtp_method() { return static_cast<Derived&>(*this).method(); } // compiles using crtp_type = typename Derived::type; // doesn't compile }; struct X : public crtp_base<X> { void method() {} using type = int; }; int main() { }

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

丶灬走出姿态 提交于 2020-01-10 04:17:27
问题 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

Why is the downcast in CRTP defined behaviour

安稳与你 提交于 2020-01-06 04:46:09
问题 I have used the CRTP pattern for a while, however reading answers about undefined behaviour related to downcasting I don't understand why the static_cast<Derived&>(this) , where this is of type Base<Derived>* is defined behaviour, where Derived inherits publicly from Base<Derived> . The standard is quite clear: static_cast < new_type > ( expression ) If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static

How to implement a compile-time check that a downcast is valid in a CRTP?

做~自己de王妃 提交于 2020-01-02 02:33:10
问题 I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast

CRTP to avoid virtual member function overhead

会有一股神秘感。 提交于 2020-01-01 06:54:20
问题 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

A polymorphic collection of Curiously Recurring Template Pattern (CRTP) in C++?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 00:52:07
问题 I've got a class Base from which I have two classes, DerivedA and DerivedB as defined below. template <typename Derived> class Base{ public: double interface(){ static_cast<Derived*>(this)->implementation(); } }; class DerivedA : public Base<DerivedA>{ public: double implementation(){ return 2.0;} }; class DerivedB : public Base<DerivedB>{ public: double implementation(){ return 1.0;} }; In short, I'm trying to do the following to maintain a collection of objects, some of which are DerivedA