The use of double include guards in C++

I am pretty sure that it is a bad practice to add another include guard like: #ifndef __HEADER_A_HPP__ #include “header_a.hpp” #endif Here are some reasons why: To avoid double inclusion it is enough to add a usual include guard inside the header file itself. It does the job well. Another include guard in the place … Read more

Array-size macro that rejects pointers

Linux kernel uses a nice implementation of ARRAY_SIZE to deal with this issue: #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) with #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) and #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) Of course this is portable only in GNU C as it makes use of two instrinsics: typeof operator and __builtin_types_compatible_p function. Also it uses … Read more

Can we have recursive macros?

Macros don’t directly expand recursively, but there are workarounds. When the preprocessor scans and expands pr(5): pr(5) ^ it creates a disabling context, so that when it sees pr again: ((5==1)? 1 : pr(5-1)) ^ it becomes painted blue, and can no longer expand, no matter what we try. But we can prevent our macro … Read more

Understanding the behavior of C’s preprocessor when a macro indirectly expands itself

Macro expansion is a complex process that is really only understandable by understanding the steps that occur. When a macro with arguments is recognized (macro name token followed by ( token), the following tokens up to the matching ) are scanned and split (on , tokens). No macro expansion happens while this is happening (so … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)