Compile-Time and Runtime-Safe Replacement for “printf”

January 29, 2015

C++ 11 is truly beautiful. And one of the ways in which it is beautiful is how you can implement a compile-time and runtime-safe version of the popular C runtime function printf. Originally, printf was implemented as a variadic function that parses its first argument (a format string) to determine how many additional arguments it should read from the stack. printf has no way of knowing how many parameters were actually provided; any mismatch means a nasty exception at runtime, or, worse, undesired output. For example: printf("%d"); printf("%s"); printf("%d %d", 42); printf("hmpf", 42); The first version will probably work and print some arbitrary stack value...
tags: ,
14 comments

Implementing std::tuple from the Ground Up – Part 4: Getting Tuple Elements

January 28, 2015

We have a constructible tuple at this point, but we don't have a way of getting tuple elements by index or in any other way. It's time to implement the main accessor -- get<> -- which we use to read and write the tuple's elements. There are two flavors of the get<> template: get by index and get by type, the latter being part of tuple's interface since C++ 14. We are going to implement the latter in terms of the former. Here are the overloads we need for get<>: template <size_t I, typename... Types> ??? get(tuple<Types...> const& tup); template <size_t I, typename......
tags: ,
one comment

Implementing std::tuple From The Ground Up – Part 3: Constructing Tuples

January 23, 2015

In the previous installment we were finally able to define what tuple derives from. As a reminder, if we have a tuple of n elements, it (indirectly) derives from n instantiations of tuple_element. For example, tuple<int, string> indirectly derives from tuple_element<0, int> and tuple_element<1, string>. We'll need to add some operations to tuple_element to make it more useful. At the very least, we need to make it constructible from its value type: explicit tuple_element(T const& value) : value_(value) {} explicit tuple_element(T&& value) : value_(std::move(value)) {} Now let's start building some fundamental operations for our tuple class so that we can get busy constructing...
tags: ,
7 comments

SDP Workshop: Monitoring .NET Performance with ETW

January 22, 2015

I've been doing .NET performance workshops at the SDP for 4 years now, and this year I thought it was time for a change. The traditional workshop used to be about a variety of commercial performance measurement tools, such as the Visual Studio profiler, and unfortunately I wasn't able to offer any labs, so it wasn't really a hands-on workshop. This time I decided to rewrite 90% of the materials and focus only on ETW tools. Here's the rough agenda of the workshop -- I'm pretty happy with the results! Introduction to semantic logging and ETW Capturing kernel ETW events with xperf...
no comments

Implementing std::tuple From The Ground Up – Part 2: Index Sequences

January 16, 2015

In the previous installment we had the following definition of tuple, and were looking for a way of improving it so that the caller doesn't have to provide the integer indices. template <size_t... Indices, typename... Types> class tuple : tuple_element<Indices, Types>... { }; tuple<0, 1, int, string> tup; Basically, we need to provide the indices ourselves. First, let's decouple the tuple class from the indices -- we don't want the client to see the additional template parameters required: template <size_t... Indices, typename... Types> struct tuple_impl : tuple_element<Indices, Types>... { }; template <typename... Types> class tuple : tuple_impl<???, Types...> { }; So... how is the tuple class going to provide tuple_impl with all the indices?...
tags: ,
5 comments

Implementing std::tuple From The Ground Up – Part 1: Introduction and Basic Structure

January 12, 2015

std::tuple is a very nice facility originally introduced in C++ TR1. It is a heterogenous container of elements that has a statically known size. In C++ 11, std::tuple can be implemented using variadic templates; a single std::tuple class can support an arbitrary number of template type arguments. In this series of blog posts we will implement std::tuple from first principles. The purpose of this exercise is not to provide the best-performing or most-conformant tuple implementation, but rather to see what foundational concepts are required to implement it. NOTE: This blog series relies on good familiarity with variadic templates and basic...
tags: ,
7 comments