Skip to main content

Posts

Showing posts from 2014

Fun with C++14 Lambdas at Silicon Valley Code Camp

Believe it or not, but the 9th Silicon Valley Code Camp is less than 2 weeks away and I can't wait to be at the largest software technology conference setup by developers for developers---and here is the best part---at no cost to the attendees. So far, there are 234 registered sessions, 7 technical tracks , and over 3100 registrations. So mark your calendar--it's October 11th and 12th, Saturday and Sunday, as always. C++ is hot again at SVCC and third year in a row there is a dedicated track for modern C++. There are 11 sessions  covering a wide variety of topics related to modern C++ programming. I wanna thank SVCC organizers who generously allowed me to present two sessions: The first one is titled: Fun with Lambdas: C++14 Style [ video ]. You may be following the Fun with Lambdas series on this blog and hopefully having some fun too! I'll present a sampling of the content discussed here with new insights. Check out  part 1 , part 2 , and part 3 if you haven

Short-circuiting overloaded && and || using expression templates

This blog post is just a quick note that C++ offers (at least) two distinct ways to represent lazy computation that is lexically in the same scope but may execute lazily at a later time. In doing so, the computation must capture the local context (i.e., variables) so that it can be used later when needed. Clearly, lambda expressions are a direct language supported mechanism for that. Closures that come out of a lambda expression often capture the context and of course some behavior to be run later. The second mechanism is about 20 years old (as of this writing): Expression Templates . Lets take an example of short-circuiting overloaded && and || operators. Regular overloaded && and || do not short circuit in C++. The reason is that before calling the overloaded operator &&, both the left-hand-side and the right-hand-side arguments of the overloaded function are evaluated. A function call is a sequence-point and therefore all the computations and the side-ef

Fun with Lambdas: C++14 Style (part 3)

Now that we have C++14 , it has opened up doors for truly mind-bending uses of lambdas--more specifically--generic lambdas. This blog post is the third installment in the series of "Fun with Lambdas: C++14 Style". Check out part 1 and part 2 if you have not already. This post is about "monadic tuples". Monad--a simple but powerful abstraction, however, considered quite difficult to understand in the imperative circles. We will look into what's know as the "continuation monad". As it turns out, in C++14, you need just a couple of lines of code to create an instance of a continuation monad. I'm fairly new to the world of monads. So, things did not begin with great clarity for me. It all started with an intriguing question on Stackoverflow . As it turns out the same "trick" is also used in Boost.Hana and discussed on boost mailing list here . What you see below is more or less how I came to understand the idiom as an instance

Using The Pigeonhole Principle in C++ Metaprogramming

The Pigeonhole Principle  is one of the most obvious fundamentals in mathematics. It is so obvious that you may be surprised that there is even a name for it. It states that: "If n items are put into m containers, with n > m, then at least one container must contain more than one item." Alternatively, "If there are n items and m containers, with n > m, and only one item can fit in a container, then at least one item must remain out." For those who prefer visuals and really hate math: Even though the principle is simple it has been used to prove many complex mathematical theorems and lemmas. Here is one I find quite interesting: "Incompressible strings of every length exist." Alternatively, "There is a file of every size that your favorite zip program can't compress." The solution is left to the reader as an exercise. So, does the Pigeonhole Principle show up in programming. Of course it does. That's why

A tale of noexcept swap for user-defined classes in C++11

C++11 has taken another stab at function exception specifications for the masses. The shiny new ‘noexcept’ keyword is phasing out its zombie cousin ‘throw’. The well accepted guideline for the old exception specification is: "don’t use them!" That’s good for us because now we don’t have to bother (the reasons for not using throw specification aren’t for the faint hearted anyways.) The noexcept feature does not appear to be a walk in a park either. So hold on tight! noexcept is meta-programmable! a.k.a conditional noexcept. It is possible to conditionally specify functions to throw any exceptions. The noexcept specification of functions can be inspected at compile-time and functions can derive their own noexcept specification based on exception specifications found elsewhere in the program. Meta-programs are not off-limits here. An excellent introduction of the noexcept feature and its history can be found in June’11 Overload Journal and on Andrzej's C++ blog . I won’

Fun with Lambdas: C++14 Style (part 2)

Look at some interesting examples of C++11/14 lambdas and how they interact with other language features and libraries. I hope to find some time to add some explanations. See part 1 if you missed it. Associative containers and lambdas std::set<int, std::function<bool(int, int)>> numbers([](int i, int j) { return i < j; }); Recursive Lambdas (see Creating recursive lambdas and returning them too! ) auto make_fibo() { return [](int n) { std::function<int(int)> recurse; recurse = [&](int n){ return (n<=2)? 1 : recurse(n-1) + recurse(n-2); }; return recurse(n); }; } Composable list manipulation (e.g., cpplinq , narl , LEESA ) Box boxes[] = { ... }; int sum_of_weights = cpplinq::from_array(boxes) >> where([](const Box & box) { return box.color == Color.RED; }) >> select([](const Box & box) { return box.get_weight(); }) >> sum(); Overloaded Lambdas templa

Why we need compile-time reflection in C++1y

Programs need data. That's a no brainer. Programs are only as good as the data you provide them. Based on what kind of data is consumed, programs can be divided into two broad categories: (1) those that operate on regular data (a file), and (2) those that operate on other programs. The first kind of programs are abundant. Your browser, for instance, is showing you this page--its data. The second kind of programs are more interesting and they are called meta-programs. Meta-programs need data too. As with the other programs, meta-programs are only as good as the data you provide them. So what do we feed them? ... Well, In C++, more important than 'what' is 'when'. (remember Morpheus?) A C++ program is just a sequence of bits the compiler is trying to understand. So, while the compiler is trying to make sense of your program, most of it gets translated (to assembly) but some of it gets executed. Quite intriguing! We're talking about compile-time meta-programming

Fun with Lambdas: C++14 Style (part 1)

It's common knowledge that Functional Programming is spreading like a wildfire in mainstream languages. Latest promoted languages: Java 8 and C++, both of which now support lambdas. So, let the lambdas begin! and may the fun be ever on your side. The same text is available in slides form on Slideshare . This blog post and the talk/slides are inspired by JSON inventor  Douglas Crockford . Write an Identity function that takes an argument and returns the same argument . auto Identity = [](auto x) { return x; }; Identity(3); // 3 Write 3 functions add, sub, and mul that take 2 parameters each and return their sum, difference, and product respectively. auto add = [](auto x, auto y) { return x + y; }; auto sub = [](auto x, auto y) { return x - y; }; int mul (int x, int y) { return x * y; }; Write a function, identityf, that takes an argument and returns an inner class object that returns that argument . auto identityf = [](auto x) { class Inner { int x; publi

Fun with Lambdas: C++14 Style

I am presenting at the SF Bay Area Association of C/C++ Users (ACCU)  meetup  on Wed, Mar 12th. Topic: Fun with Lambdas: C++14 Style. Slides and the blog will be available here so stay tuned.