Stupid C++ trick(s)

(idea shamelessly stolen from Jim Tilander and Power of Two Games. Not fullblown post, just random notes from time to time, as I learn/think about it)

  • how to easily make sure all our header are self-contained? Include header as the first one in corresponding .cpp file. For example (ThreadPool class, ThreadPool.cpp file):

    #include "thread/ThreadPool.h"
    
    #include "thread/LockFreeMemList.h"
    
    #include "thread/LockFreeQueue.h"
    
    #include "thread/Task.h"
    
    #include "thread/TaskGroup.h"
    If my coding standard could only have single rule - it would be this one.

  • SFINAE (Subsitution Failure Is Not An Error). That’s a neat little “feature” of C++, where invalid substitution of template parameters is not an error in itself. It can be abused for all kinds of crazy compile-time checks. Say, we want to test if type T has an internal _keytype typedef.

    template char HasKeyType(...);
    template int HasKeyType(typename T::key_type*);
    bool hasKeyType =
        sizeof(HasKeyType<std::map<int, int> >(0)) != sizeof(char);

  • MSVC specific - how to make sure you didnt forget to disable exceptions/RTTI for all your libraries? It turns out that Visual C++ defines macros that let you check it.

    • _CPPUNWIND - if exceptions are enabled,
    • _CPPRTTI - if RTTI is enabled.
More Reading
Older// Firefox 3