Creating circular generic references

Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this: public interface P2PNetwork<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void addClient(C client); } public interface P2PClient<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void setNetwork(N network); } class TorrentNetwork … Read more

How to avoid errors while using CRTP?

In C++0x you have a simple solution. I don’t know whether it is implemented in MSVC10 however. template <typename T> struct base { private: ~base() {} friend T; }; // Doesn’t compile (base class destructor is private) struct foo : base<bar> { … };

CRTP — accessing incomplete type members

When a class template is instantiated, its members other than non-virtual member functions are instantiated together with it. Non-virtual member functions, however, are only instantiated when odr-used (basically, called or have their address taken). When the compiler encounters class Implementation : public Interface<Implementation>, it needs to instantiate Interface<Implementation>. At this point, Implementation is still an … Read more

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

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