invalid use of incomplete type

The reason is that when instantiating a class template, all its declarations (not the definitions) of its member functions are instantiated too. The class template is instantiated precisely when the full definition of a specialization is required. That is the case when it is used as a base class for example, as in your case. … Read more

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

The assignment operator is technically inherited; however, it is always hidden by an explicitly or implicitly defined assignment operator for the derived class (see comments below). (13.5.3 Assignment) An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a a … Read more

CRTP to avoid dynamic polymorphism

There are two ways. The first one is by specifying the interface statically for the structure of types: template <class Derived> struct base { void foo() { static_cast<Derived *>(this)->foo(); }; }; struct my_type : base<my_type> { void foo(); // required to compile. }; struct your_type : base<your_type> { void foo(); // required to compile. }; … Read more

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

derived is incomplete when you use it as a template argument to base in its base classes list. A common workaround is to use a traits class template. Here’s your example, traitsified. This shows how you can use both types and functions from the derived class through the traits. // Declare a base_traits traits class … Read more

Java Enum definition

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 called StatusCode, it would be equivalent to: public class StatusCode extends Enum<StatusCode> Now if you … Read more

How to self register class instances using the CRTP?

I decided to reopen the question and self answer based on Yakk’s fixes. Seems to be a more common problem (see here for example) Yakk’s example solved it: #include <cstdint> enum class CommandId : uint16_t { Command1 , Command2 , }; #include <map> #include <functional> #include <string> //#include “CommandId.h” class Registry { public: static std::map<CommandId,std::function<void … Read more

tech