This time I would like to focus on some aspect that every C/C++ programmer is familiar with. That is preprocessor conditions. Before being crucified because of using #ifdef and C++ in the same sentence. I would like to confess, that this is still something frequently used.

What is #ifdef?

#ifdef is a preprocessor condition, that includes containing code whether condition predicate solves to ‘true’. It might be useful to make sure some code will be never compiled.

For the instance let’s see a code

void HandleResponse(const CHTTPResponse& resp) {
#ifdef WITH_CONTENT_VALIDATOR
if (!Validate(resp.content(CHTTPResponse::Text))) {
    ForwardCorruptedResponse(resp);
}
#endif
// ...
}

So far so good, let’s just compile our project with: g++ -DWITH_CONTENT_VALIDATOR=1 and see if everything works. It does. Sweet! No tests are required now, so let’s disable them by setting -DWITH_CONTENT_VALIDATOR=0 and check. Wait! My tests are still executed!

 
13 Kudos
Don't
move!