Integer binary representations

Integer binary representations I had never had to look closely at integer binary representations in computers. The mental model I had for them was not wrong, but it turns out it was sub-optimal and there are better ways to do things. If you use high level abstractions and do not mainly work with fundamental types, or if you do not convert between integer types, you do not have to be mindful of the binary representation of

Continue Reading »

Friends only class

Friends only class Having read a few blog posts and watched a short presentation on the subject of strong typedefs, I decided to look into their use and implementation. In at least two implementations I have looked at (namely type_safe and opaque), I have found that mixin classes are used to add functionality to the new type. For instance: class my_strong_typedef : public addable, public divisible {}; Those mixin were implemented as empty classes that only have friend functions

Continue Reading »

Count chars in lines

Counting chars in first n lines I was coding in one of my projects and wanted to make sure that the position I was finding in a file was correct. In a first attempt, I tried copy-pasting the content of the file up to the position I was searching for into Microsoft Excel™ to find the length of the resulting string. Turns out a naive copy-paste does not preserve whitespace. I then thought of writing a small

Continue Reading »

Return type overloading

No return type overloading In C++, return type does not participate in function overload resolution, i.e. it is not possible to overload a function on the return type. Thus, this is not legal C++: void to_lower( std::string& strg ); std::string to_lower( std::string& strg ); The compiler will issue an error when it sees the second declaration.1 For instance, the error Clang emits is the following: "error: functions that differ only in their return type cannot be overloaded". The detailed reasons

Continue Reading »

rvalue references in C++

move, rvalues, forward and C++ Ever since I heard about it, the concept of move semantics has been intriguing and appealing to me. I confess to liking new C++ things and micro-optimizations way too much... but still, the concept of "moving" memory instead of copying it in order to gain efficiency is at least worth exploring, right? Anyhow, I chose a function of mine and decided to try and make it handle move semantics properly. The function

Continue Reading »