How to Handle Circular Dependencies with Templates in C++
Comments
@codenine75a - The article isn't about event loops. It's about how to avoid circular dependencies with templates in C++.
@codenine75a: As Dave Hunt said, this article was about handling circular dependencies with templates in C++, and thus the code samples are at a bare minimum.
I chose to use real world names for the class names, because I personally can't follow advanced code written like "class Foo" and "class Bar".
However, I'm planning some articles about messages and how to use implement and use them in a game. I will not make any promises, but I hope to get them done this year :).
@Freya Yeah, it is especially awful when I see code written using "Class A", Class B", "int a"....etc
BTW I did enjoy the article.
Just a note about one comment in the article which is misleading. "It's not possible to split the declaration and definition into separate files", it is possible, you just have an include after the declarations or include the files in order from another header. This is fairly common practice with things such as Boost where you put the definition portion of the code in a 'details' directory. It is not a h versus c file styled break but it does allow you to break the code into logical separate units for well organized declaration and definition portions.
Visual Studio is trying to be smart. It can see forwards and there is an implementation (definition) of the Entity class further down below. GCC (g++) is not trying to be smart, and thus the code above fail to compile with g++.
It's actually kind of the other way around. Visual Studio is still to this day missing two-phase lookup. It works not because it's smart but because it's incorrectly delaying all semantic analysis of the template until instantiation, which while solving issues like this causes other bugs.
You could actually just modify your templates slightly to make GCC and other standards-conforming compilers happy by making the type of Entity a template parameter to the message handler functions (and hence a dependent type). Then there will be no semantic checks on the entity parameter until instantiation and everything will work.
Just a note about one comment in the article which is misleading. "It's not possible to split the declaration and definition into separate files",
It's also possible for template specializations, which granted are only used or useful in certain cases.
This is a problem I encountered while tuning the Message-Component-Entity system in our game under development while using C++ templates
Why not optimize and use virtual function pointers. The events can be handled through a simple array of functions labeled with maybe enum or something. I never liked event loops.