crtp

Implicit conversion of lefthand argument in in-class declared friend operator

爱⌒轻易说出口 提交于 2019-12-10 22:03:55
问题 I am using CRTP to provide template-argument dependent addition of functions to a class, in this case the addition of operator + and operator += , using the template class ImplAdd . For the former, implicit conversions should be performed on both arguments, which means I have to use an in-class friend operator like this: template<class Type, bool active> struct ImplAdd{ virtual int get_val_() const = 0; virtual void set_val_(int) = 0; }; //if activated is true, the operators + and += will be

Will concepts lite change the need of CRTP to achieve static polymorphism?

二次信任 提交于 2019-12-10 17:23:38
问题 Since I have discovered CRTP some years ago, I use it in many places to achieve compile-time polymorphism for very intensive computing oriented codes. It's great to "inject" member functions into classes in a generic way when one cares about both genericity and maximal performances at runtime. I have read/watch several things on the concepts lite which will be (I hope) a part of the next C++ standard. It will be absolutely wonderfull to design functions in a more abstract and generic manner,

CRTP refer to typedef in derived class from base class

好久不见. 提交于 2019-12-10 15:03:21
问题 I have following code: template <typename T> class A { typedef typename T::Type MyType; }; template <typename T> class B : public A<B<T>> { typedef T Type; }; When I try to instantiate B, I get following error message using MSVS 2015: 'Type': is not a member of 'B<int>' Is this code valid C++ or is MSVS right? 回答1: The problem is at this point template <typename T> class A { typedef typename T::Type MyType; ^^^ }; T needs to be a complete type. But in your case, when A<T> is instantiated here

Clang++-3.7 CRTP compilation error “no named member” in parent's template argument

梦想与她 提交于 2019-12-10 14:59:35
问题 In the below code I am trying to use CRTP to use the static member "value" from the Child class in the Parent class. When compiling the code with g++ 5.2.1 with the "-pedantic" flag, I am able to compile as expected, and on execution both c.print_value(); and Child<int,4>::print_value(); print out 4. #include <iostream> template <typename DE> struct Parent { static const int value = DE::value; static void print_value () { std::cout << "Value : " << value << '\n'; } }; template <typename T,

Use of member of derived type in CRTP class

Deadly 提交于 2019-12-10 14:54:49
问题 I have a curiously recurring template pattern class and a derived class like so: template<class Derived> class A { typedef typename Derived::C D; D x; }; class B : public A<B> { public: class C { }; }; This fails to compile due to B not being fully defined when the compiler attempts to define D. How can I achieve a similar result, i.e. have members of A that are of a type defined in B? Or do I have to force C to be defined outside of B? 回答1: Or do I have to force C to be defined outside of B?

Create alias for a list of types and passing it as a template parameter

萝らか妹 提交于 2019-12-10 13:04:17
问题 I am using variadic templates to implement the visitor pattern: template<typename... Types> class Visitor; template<typename Type> class Visitor<Type> { public: virtual void visit(Type &visitable) = 0; }; template<typename Type, typename... Types> class Visitor<Type, Types...>: public Visitor<Types...> { public: using Visitor<Types...>::visit; virtual void visit(Type &visitable) = 0; }; template<typename... Types> class VisitableInterface { public: virtual void accept(Visitor<Types...>

Overload operator in template base class

…衆ロ難τιáo~ 提交于 2019-12-08 07:50:54
问题 I've got the following code: #include <iostream> #include <cstring> #define THIS(X) static_cast<X*>(this) template<typename T> class Base { public: Base() { } virtual ~Base() { } void foo() { THIS(T)->a[0] = 1; } T& operator=(const T& o) { std::cout <<"operator="<<std::endl; memcpy(THIS(T)->a, o.a, THIS(T)->size); return static_cast<T&>(*this); } }; template<typename T> class Der1: public Base<Der1<T> > { private: T* a; unsigned int size; public: Der1(int d) { a = new T[d]; size = d; }

How to use CRTP with variadic templates?

拜拜、爱过 提交于 2019-12-08 06:19:16
问题 Let's suppose originally I have the following design using CRTP: template<class Outputter> class Generator { protected: vector<int> v; private: void work(ostream& out) { // perform first part of some complex operations on v out << *static_cast<Outputter *>(this); // perform second part of some complex operations on v out << *static_cast<Outputter *>(this); // many more .... // perform some final actions } public: Generator(unsigned length): v(length) {} friend ostream& operator<<(ostream& out

How can I implement “op” in terms of “op=” in a CRTP base class?

百般思念 提交于 2019-12-08 03:17:12
问题 Herb Sutter's Guru of the Week #4, "Class Mechanics", teaches that the "a op b" form of an overloaded operator should be implemented in terms of the "a op= b" form (see point #4 in the solutions). As an example, he shows how do to this for the + operator: T& T::operator+=( const T& other ) { //... return *this; } T operator+( T a, const T& b ) { a += b; return a; } He points out that first parameter in operator+ is intentionally being passed by value, so that it can be moved if the caller

Can CRTP completely replace virtual functionality for smaller designs?

喜欢而已 提交于 2019-12-08 01:01:47
问题 Is CRTP capable enough to outsmart virtual functionality completely ? The only disadvantage I see with CRTP is notable amount of code generated for every recurring pattern. For smaller designs, (where 2-3 classes are derived from a base), is CRTP a better idea ? 回答1: CRTP does not provide runtime polymorphism . If you need runtime polymorphism, you need virtual methods. Worse, since the base class is templated, you can't even really use the subclass objects as if they were of the same type as