devxlogo

Avoiding Dependencies #1

Avoiding Dependencies #1

Many C++ programmers will be familiar with the tidal wave of recompilations that occur when a seemingly unrelated header file is changed. C++ file dependencies need to be managed otherwise compilation times will grow unchecked. Large build times can cripple the process of creating software and eventually kill a project.
One tip that can help sometimes is to remove the #include and use a template instead. Here’s an example.
Suppose you’ve written a small framework that tokenizes a source file. The representation of a token need not be the obvious string; it could be two iterators that mark the beginning and end of the token as it appears in the whole source file, stored somewhere else as a vector of chars:

 #include namespace source_file{    class token    {    public: // types        typedef std::vector container;        typedef typename container::iterator iterator;        ...    public: // 'tors        token(iterator begin, iterator end);        ...    private: // state            const iterator begin, end;    };}

size=3>
Programmatically, the #include in this fragment is required because the token class stores vector::iterators by value (and even if it didn’t there’s no way to forward declare a nested class, and anyway the ISO C++ standard prohibits you from explicitly forward declaring anything in the standard C++ library). The slight feel of

devx-admin

Share the Post: