How to Handle Circular Dependencies with Templates in C++

Published January 16, 2014 by Kent Fagerjord, posted by freyastudio
Do you see issues with this article? Let us know.
Advertisement
This is a problem I encountered while tuning the Message-Component-Entity system in our game under development while using C++ templates
https://studiofreya.com/cpp/how-to-handle-circular-dependencies-with-templates-in-c
Cancel Save
0 Likes 7 Comments

Comments

codenine75a

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.

December 17, 2013 02:18 PM
Dave Hunt

@codenine75a - The article isn't about event loops. It's about how to avoid circular dependencies with templates in C++.

December 17, 2013 06:48 PM
freyastudio

@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 :).

December 17, 2013 09:11 PM
lask1

@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.

December 18, 2013 04:49 AM
All8Up

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.

December 18, 2013 06:17 AM
SeanMiddleditch

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.

December 18, 2013 07:32 PM
SeanMiddleditch

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.

December 18, 2013 07:34 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

This is a problem I encountered while tuning the Message-Component-Entity system in our game under development while using C++ templates

Advertisement

Other Tutorials by freyastudio

freyastudio has not posted any other tutorials. Encourage them to write more!
Advertisement