crtp

How to implement CRTP functionality in python?

谁说胖子不能爱 提交于 2019-12-12 16:09:46
问题 I want to access a member (variable) of a derived class from a base class in python. In c++ I can use the CRTP design pattern for this. In c++, for example, I would do something like this: #include <iostream> template <class derived> class Base { public: void get_value() { double value = static_cast<derived *> (this) -> myvalue_; std::cout<< "This is the derived value: " << value << std::endl; } }; class derived:public Base<derived>{ public: double myvalue_; derived(double &_myvalue) {

clang vs gcc CRTP: constexpr variable cannot have non-literal type

亡梦爱人 提交于 2019-12-12 09:46:32
问题 I have a CRTP template class here: template <typename S> class Base { public: constexpr static S NOT_SET{0}; }; struct Derived : public Base<Derived> { }; Clang (5.0.0) does not accept this: 5 : <source>:5:24: error: constexpr variable cannot have non-literal type 'const Derived' constexpr static S NOT_SET{0}; ^ 8 : <source>:8:25: note: in instantiation of template class 'Base<Derived>' requested here struct Derived : public Base<Derived> ^ 5 : <source>:5:24: note: incomplete type 'const

C++: With CRTP, class defined in the derived class is not accessible in the base class

不羁的心 提交于 2019-12-12 08:51:29
问题 Here is the (simplified) base class: template <class T> class SharedObject { protected: QExplicitlySharedDataPointer <typename T::Data> d; }; And here is the derived: class ThisWontCompile : public SharedObject <ThisWontCompile> { private: friend class SharedObject; struct Data : public QSharedData { int id; }; }; Is there any workaround to access ThisWontCompile::Data from SharedObject ? What exactly can and what exactly cannot be done with the derived object from the base object? 回答1: This

Fix circular dependency in arithmetic class

柔情痞子 提交于 2019-12-12 03:33:36
问题 I have a set of classes implementing the curiously recurring template pattern. However, the trick is that the base class needs to return instances of the subclasses. Here's an example: template <typename SubType> class ArithmeticBase { public: template <typename OtherType> const Addition operator+(const OtherType &other) {return Addition(get_subclass(), other);} // ... // Operators for subtraction, multiplication, division, ... private: const SubType &get_subclass() const {return *static_cast

C++ Trait for Function Existence Is Not Working With CRTP

醉酒当歌 提交于 2019-12-11 17:43:43
问题 I'm trying to solve this problem : how to evaluate if a class has a specific function with CRTP. I got this as my class trait evaluation : template <typename T> class function_exists_trait { private: template <typename U> static auto test_todo(U * u) -> decltype(&U::exists) {} static auto test_todo(...) -> std::false_type {} public: static constexpr bool value{ !std::is_same<decltype(test_todo(static_cast<T*>(nullptr))), std::false_type>::value }; }; And i tested it with some trivial classes

Declaring a function with same signature of the given template parameter's function

帅比萌擦擦* 提交于 2019-12-11 09:10:06
问题 I'm writing a wrapper class to be derived which hides the implementation. How can I get the signature of the given template parameter's function? template <class T> struct wrapper { static typename std::result_of<&T::impl>::type call(...) { // this function has the same signature of T::impl(); // here goes the jobs to do, such as logging or something return T::impl(...); } }; struct sum : public wrapper<sum> { private: friend class wrapper<func> static int impl(int a, int b, int c) { return a

Reference counting with a generic intrusive pointer client

亡梦爱人 提交于 2019-12-11 08:46:55
问题 Introduction Peter Weinhart describes how to design a generic intrusive_ptr base class using CRTP, which may be used as follows: class foo : intrusive_base<foo> { // foo-specific code. }; This approach imposes the constraint that all foo objects carry a reference counter. Assume that we keep foo sometimes by value and only want to pay the price of the reference counter when we have a pointer. For example, sometimes we would like to create foo instances and just move them around, and sometimes

Error when declaring static constexpr instance of derived class in CRTP base class

亡梦爱人 提交于 2019-12-11 05:28:27
问题 I'm building a container type and attempting to reuse as much code as possible using the Curiously Recurring Template Pattern. Here's the base code, which compiles in some cases, but not in others: template<typename T, size_t N, typename ConcreteClass> struct _BaseClassOperators { const ConcreteClass& operator+=(const ConcreteClass& other) { ConcreteClass& self = static_cast<ConcreteClass&>(*this); for(size_t i = 0; i < N; ++i) { self.data[i] += other.data[i]; } return self; } const

Does “friending” the base class in CRTP inheritance affect the child class as well?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 04:49:47
问题 In an attempt to answer another question, I came up with a scheme to force children of a CRTP base class to accept a particular type as a parameter in their constructors: make the parameter type's constructor private , assign the CRTP base class as a friend , and declare the parameter type as a parameter for the base class constructor as well. However, when I tried to demonstrate that this scheme provided the desired protections via access violations, I found that even though the parameter

C++ cannot find type defined in template base class which inherits from the current template class

*爱你&永不变心* 提交于 2019-12-11 00:47:04
问题 I'm trying to write a variation of the template class defining a super type idiom. The class Inherit introduces the type Super to denote the possibly very long super type, and also needs to know the derived type New to do some extra things which I'm not showing here. This works fine if the type passed to New is not a template, but fails with templates. Here is a full example compiled with clang++-3.8 -std=c++1y -Wall (gcc gives the same output): struct SomeBase {}; template<class New, class