Posts

Showing posts from March, 2014

C/C++ Programming Tips and Tricks

Every once in a while I found some C/C++ tips and tricks. This page is going to be the repository of those tips and tricks. Constexpr Constexpr specifier declares that it is possible to evaluate the expression at compile time: 1. Constexpr function 2. Constexpr constructor 3. Constexpr variable Initialization of constexpr MUST happen at compile time (const variable can defer at runtime). Constexpr implies const and const implies static. If constexpr variable is in the header, every translation unit will get its own copy. Since C++17, inline keyword can be added to variables (and constexpr variables) that means there should only be one single copy in all translation units (this also allows to declare non-const variable in header file). Fast insertion to std::map/std::unordered_map std::unordered_map > m; auto [iter, succeed] = m.emplace( key, nullptr ); if ( succeed ) iter->second = std::make_unique (); Creating C++ function similar to printf() template inline st