Destructuring Standard Containers in C++17

A comment was made on my YouTube channel that destructuring in C++17 would be much more useful if it worked with standard containers. At the moment I responsed that this would be someone impossible to get right without adding too much overhead.

C++ Weekly - Ep13 Fibonacci: You're Doing It Wrong

Many of the different ways you might implement a Fibonacci sequence in C++ plus one you’ve probably never considered.

Folds (ish) In C++11

It is possible to perform some form of the variadic folds that are coming in C++17 with the C++11 and C++14 compilers that we have available today by using just a little bit of creativity.

C++ Weekly - Ep10 Variadic Expansion Wrap-Up

In this episode Jason wraps up what we’ve learned so far about variadic templates and shows some additional techniques to consider.

C++ Weekly - Ep6 Intro To Variadic Templates

In this episode I give a brief introduction to C++ variadic templates and cover some compile time and runtime performance considerations.

C++ Weekly - Ep4 Variadic Template Refactor

Template Code Bloat Revisited: A Smaller make_shared

Back in 2008 I wrote an article on template code bloat. In that article I concluded that the use of templates does not necessarily cause your binary code to bloat and may actually result in smaller code! This ended up becoming one of my more significant articles and has been referenced on wikipedia.

C++ Partial Specialization of Templates

In this article we are going to introduce the concept of C++ Template Partial Specialization. This is meant to be just a primer on the topic and not exhaustive. The examples here will be used and referenced in later articles. A series of discussions about C++11, now that the language has been finalized, will be coming shortly. In C++ a template class such as this:

When to use class vs. typename in a Template Declaration

When declaring a template you can choose either “class” or “typename” for a template parameter. Example:

Project Euler - Problem 1 - Sum All Integers Below 1000 and Divisible By 3 or 5

The first Project Euler problem is to calculate the sum of all integers below 1000 which are divisible by either 3 or 5. My solution is implemented entirely in C++ templates. The value is recursively calculated at compile time. The template specialization struct Problem1<0> stops the recursion and returns 0. To compile this code with gcc you must expand the maximum allowed template recursion depth to at least 1000. ` g++ EulerProblem1.cpp -ftemplate-depth-1000`

Simple C++ String Conversions

The boost::lexical_cast<> utility is a handy way of converting objects to and from strings, providing a mechanism that many scripting languages have built in. lexical_cast works with any C++ type that has ostream and istream operators defined for it, that is, any object that cout << object; or cin >> object; would work with. This boost facility also does intelligent things such as throwing a bad_cast exception if the operation causes an error flag on the stream to be set. I use boost::lexical_cast<> throughout my code at work for things like serialization of objects for human readable communications. It is also sprinkled liberally throughout my code for handling things like quick debug logging:

Templated Constructors in C++: Using the "explicit" Keyword

Sometimes, in the course of C++ template based programming it might be desirable to have a constructor that is templated, like the following contrived exampled:

Swig Starter Kit 0.0.2

Release 0.0.2 of Swig Starter Kit was just released. This release sees the addition of template usage examples, including custom function templates and STL usage. Using a SWIG template declaration we are able to instantiate a specific template and use it from our script code.

Pass By Iterator

Pass by iterator is not a new concept, but one that we are going to perhaps give a new name to today and propose as a standard for normal usage. If you take for example the normal way of passing around standard containers:

Nobody Understands C++: Part 5: Template Code Bloat

On occasion you will read or hear someone talking about C++ templates causing code bloat. I was thinking about it the other day and thought to myself, “self, if the code does exactly the same thing then the compiled code cannot really be any bigger, can it?” Here are the test cases presented, first without the use of templates, second with the use of templates. Exactly the same functionality and exactly the same code output:

Multithreaded C++: Part 4: Futures and Other Thread Handlers

We’ve covered the “Assembly Language”, “C” and “C++” of the C++ threading world, and now we are going to try and move beyond that.

C++ Mutiple Dispatch!

In my last posting about C++ Multiple Dispatch I wondered if it was really any different than function overloading. I now appreciate that it is something that needs to occur at runtime, not compile time.

Nobody Understands C++: Part 3: Templates

C++ templates is a huge topic that we will not fully cover here. While we have covered templates in the past, this article will cover the very basics and the reasons why we would want to use templates.

C++ Templates: Euclid's Algorithm

A while back I was going though the first chapters of Knuth’s The Art of Computer Programming and came across Euclid’s algorithm for finding the greatest common denominator. I decided to implement the algorithm as a C++ template. Here’s the complete example.

C++ Templates: Using Templates to Generate the Fibonacci Sequence

The follow code demonstrates a method for generating the Fibonacci Sequence at compile time. Since the sequence is generated at compile time runtime execution of retrieving a number is constant time.

Boost Preprocessor Metaprogramming and Intro to Templates Revisited

Say you come up with a clever idea for initializing standard container types (blatantly stolen from the C++0x Initializer List concept). It might looks something like the implementation below:

Automatically Generating Templates with the Boost Preprocessor Metaprogramming Library

boost contains a preprocessor metaprogramming libary. What this means, simply, is that it is possible to write code which generates code. The full docs are here.

Templates in the Real World

Let’s say I’m doing some cryptography work. I’ve just received a signed message from another machine and I want to validate the signature. I have a few options: