crtp

invalid use of incomplete type

让人想犯罪 __ 提交于 2019-11-26 10:34:29
问题 I\'m trying to use a typedef from a subclass in my project, I\'ve isolated my problem in the example below. Does anyone know where I\'m going wrong? template<typename Subclass> class A { public: //Why doesn\'t it like this? void action(typename Subclass::mytype var) { (static_cast<Subclass*>(this))->do_action(var); } }; class B : public A<B> { public: typedef int mytype; B() {} void do_action(mytype var) { // Do stuff } }; int main(int argc, char** argv) { B myInstance; return 0; } This is

operator= and functions that are not inherited in C++?

狂风中的少年 提交于 2019-11-26 09:00:34
问题 Until a test I\'ve just made, I believed that only Constructors were not inherited in C++. But apparently, the assignment operator= is not too... What is the reason of that ? Is there any workaround to inherit the assignment operator ? Is it also the case for operator+= , operator-= , ... ? Are all other functions (apart from constructors/operator=) inherited ? In fact, I encountered this problem as I was doing some CRTP : template<class Crtp> class Base { inline Crtp& operator=(const Base

C++ static polymorphism (CRTP) and using typedefs from derived classes

一曲冷凌霜 提交于 2019-11-26 02:08:04
问题 I read the Wikipedia article about the curiously recurring template pattern in C++ for doing static (read: compile-time) polymorphism. I wanted to generalize it so that I could change the return types of the functions based on the derived type. (This seems like it should be possible since the base type knows the derived type from the template parameter). Unfortunately, the following code won\'t compile using MSVC 2010 (I don\'t have easy access to gcc right now so I haven\'t tried it yet).

Java Enum definition

不问归期 提交于 2019-11-26 00:24:52
问题 I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum: class Enum<E extends Enum<E>> Could someone explain how to interpret this type parameter? Bonus points for providing other examples of where a similar type parameter could be used. 回答1: It means that the type argument for enum has to derive from an enum which itself has the same type argument. How can this happen? By making the type argument the new type itself. So if I've got an enum

What is the curiously recurring template pattern (CRTP)?

試著忘記壹切 提交于 2019-11-25 21:52:58
问题 Without referring to a book, can anyone please provide a good explanation for CRTP with a code example? 回答1: In short, CRTP is when a class A has a base class which is a template specialization for the class A itself. E.g. template <class T> class X{...}; class A : public X<A> {...}; It is curiously recurring, isn't it? :) Now, what does this give you? This actually gives the X template the ability to be a base class for its specializations. For example, you could make a generic singleton