Wrapping Up DevWeek 2015

Thursday, April 2, 2015

Just a couple of months ago, I agreed to deliver eight breakout sessions and a full-day workshop at DevWeek 2015. And no, I don't have any regrets -- but it was definitely a very packed week with lots of room changes and, more importantly, context switches from one topic to another. If you've been to DevWeek this year, I'm sure you enjoyed it: it's getting better year over year, and this is my third one so far. Below you can find the materials for my eight sessions. If you've been to my workshop and haven't got the materials, please contact...

DevWeek Workshop: Making The Most Of C++ 11/14

Thursday, March 19, 2015

It's been a few weeks since my last post. Should I apologize? I really should. But with eight sessions and a workshop at DevWeek, I hope to make up for it in the upcoming weeks. Anyway, next week I'm giving a workshop at DevWeek, titled Making The Most Of C++ 11/14. It's a distilled summary of modern C++ that every C++ developer has to be aware of. This is a slightly longer description that what was published on the conference website. The C++ language many of us are using today goes back more than thirty years. You might be using some “newer” features, such...
tags: ,
no comments

Implementing std::tuple From The Ground Up: Part 7, tuple_cat Take 2

Sunday, February 22, 2015

This is the last post in the series. We have a fully functional tuple class, with a decent implementation of tuple_cat -- but some reservations about its performance. In this post we're going to improve it considerably, at the expense of making the implementation more difficult. The main problem with what we did in the previous post is that we are creating many temporary tuple objects. When concatenating n tuples, we create n-2 intermediate tuples that hold partial results. But can it be avoided? UPDATE (Feb 24): I forgot to credit Stephan T. Lavavej (a.k.a. STL, also the maintainer of Microsoft's STL)...
tags: ,
no comments

Implementing std::tuple From The Ground Up: Part 6, tuple_cat Take 1

Friday, February 13, 2015

We are almost done. We have a pretty functional tuple that supports almost every operation mandated by the Standard. With one major exception: tuple_cat. First, let's see what it does: auto t1 = tuple_cat(make_tuple(1), make_tuple(2)); // tuple<int, int>(1, 2) auto t2 = tuple_cat(t1, make_tuple(3), make_tuple(4)); auto t3 = tuple_cat(t1, t1, t2, t2); // t3 is a tuple of 12 ints: 1, 2, 1, 2, 1, 2, 3, 4, 1, 2, 3, 4 Hmpf. Obviously, tuple_cat is a variadic function template. tuple is itself a variadic class template, and we're now trying to build a variadic function template that takes any number of variadic class...
tags: ,
one comment

Implementing std::tuple from the Ground Up – Part 5: Tuple Non-Member Functions

Tuesday, February 3, 2015

After doing all the heavy lifting in the previous four installments (, , , ), this one is going to be very lightweight. We will implement a few simple tuple non-member functions and helpers. First, let's implement a very simple helper: tuple_size. It is a metafunction that returns the number of elements in the tuple. Exercise 11: Implement tuple_size. Solution: template <typename> struct tuple_size; // undefined base template template <typename... Types> struct tuple_size<tuple<Types...>> : std::integral_constant<size_t, sizeof...(Types)> { }; Well, that was easy. Let's do another: forward_as_tuple. The basic idea is that you provide a set of elements and get back a tuple that has lvalue or rvalue references depending...
tags: ,
one comment

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

Thursday, 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

Wednesday, 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: ,
no comments

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

Friday, 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: ,
one comment

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

Friday, 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

Monday, 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: ,
6 comments