C++ minimal std::any

A few days ago I wrote a simple alternative to std::any. It is heavily inspired by Sean Parent’s talk Better Code: Runtime Polymorphism. If you don’t know what std::any is, basically it is a structure that can hold data of any type, while maintaining the value semantics. With C++11 this is really easily done in just a few lines. I’m still unsure how and where can I use it. // "struct object" is the magic type. [Read More]

C++ Code Snippets 1

This is a set of some code snippets that you might find useful. The 1st one is pretty popular, but I use it in the snippets below. Basically it is A way to retrieve the size of a C++ array. template <typename T, size_t N> char (&TArrSize_Safe(T (&)[N]))[N]; #define ARRSZ(A) (sizeof(TArrSize_Safe(A))) // Usage int myArray[1234]; printf("myArray has %d elements!" ARRSZ(myArray)); printf style std::string formatting: // The caller is EXPECTED to call va_end on the va_list args inline void string_format(std::string& retval, const char* const fmt_str, va_list args) { // [CAUTION] // Under Windows with msvc it is fine to call va_start once and then use the va_list multiple times. [Read More]