I am working on an API in C++ and trying /really/ hard not to use RTTI, (Run-Time Type Information), to implement some dependency injection functionality.
I believe I can do this, but by utilizing templates–but it got me wondering:
When the templates are “expanded” and implemented by the compiler, do they in fact introduce a circular dependency, (either compile time or run time), when the template class is implemented in a framework library, and the client of that class is in a upper tiered library?
Thanks for your help!
#include <string>
/************************************************************/
// Implemented in Framework.lib
namespace Framework
{
template<typename ShapeTemplateType>
class Utility
{
void Do()
{
ShapeTemplateType x;
(void) x;
}
};
} // End namespace Framework
/************************************************************/
// Implemented in Application.lib
namespace Application
{
class StateObject
{
int i;
};
class Facade
{
Framework::Utility<StateObject> state;
};
} // End Namespace Application
/********************************************************************/
// Implemented in Client
int main(int args, char* argv[])
{
Application::Facade facade;
//Derived d;
return 0;
}
The code for the template is generated in the compilation unit where it is instantiated. In your example, the resulting machine code will be in
Application.lib.