Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Dec 1, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 <vector>

namespace source_file
    class token
    public: // types

        typedef std::vector<char> container;
        typedef typename container::iterator iterator;
    public: // 'tors

        token(iterator begin, iterator end);
    private: // state
        const iterator begin, end;

Programmatically, the #include <vector> in this fragment is required because the token class stores vector<char>::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
Jon Jagger
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date