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 in them. I was wondering what was the use of such constructs. Turns out, it is an application of the Barton–Nackman trick to allow argument dependent name lookup (ADL) to find the function.

When the compiler sees the following expression:

lt + rt

it must lookup the operator+ function to use with the types of lt and rt. From my understanding, it is equivalent to an unqualified call to the operator, as in:

operator+( lt, rt )

The various name lookup rules of C++, which are not so simple, are then used to find which function to call and an important aspect in this case is that during ADL:

namespace-scoped friend functions (and function templates) that are declared in an associated class are visible through ADL even if they are not visible through ordinary lookup

which is a quote from the cppreference website. Because of that part of the standard, an operator defined as a friend in a class of which your class inherits is found. With this trick, you can, as other libraries have done, create multiple mixin classes from which your primary class inherits and ADL will find the functions. The empty classes should mostly (if not completely) be optimized away by the compiler.

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 application to do what I wanted : open a file, read the n first lines and find the total number of characters (bad reflex, I know). In trying to figure out the command line interface for this, I realized first that it would be a lot of work for something that I might not use often and second, that there might be an easier way on a *Nix system.

This lead me to simply using the applications head and wc as follows:

head -n <line_count> | wc -m

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”.

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.

Continue Reading »

Using a theme with Hugo

Hugo themes Initially, I had two objectives for this blog. One was to have a place where I could write some notes on programming for myself so that I could find them when I needed them. The other was to learn a bit more about the web. I thought (naively, granted) I could write it all myself using a static site generator. I wanted to write the HTML, the CSS, the JavaScript, everything!

Continue Reading »