July 2017

Quick Q: How to make my custom type to work with “range-based for loops”?

Quick A: Create member functions begin() and end() returning an iterator.

Recently on SO:

How to make my custom type to work with “range-based for loops”?

The standard has been changed since the question (and most answers) were posted in the resolution of this defect report.

The way to make a for(:) loop work on your type X is now one of two ways:

  • Create member X::begin() and X::end() that return something that acts like an iterator
  • Create a free function begin(X&) and end(X&) that return something that acts like an iterator, in the same namespace as your type X.

And similar for const variations. This will work both on compilers that implement the defect report changes, and compilers that do not.

Quick Q: Initializing a C++11 string with {}

Quick A: It calls the initializer_list constructor that has the same effect in this case.

Recnetly on SO:

Initializing a C++11 string with {}

The {} initialization syntax is known as the uniform initialization syntax, it has a few key differences, but in your code as is they both do the same thing - construct a std::string object from the string literal "Test"

Initializing an object with an assignment is essentially the same as putting the right hand side in parentheses and constructing the object. For example the below two are the same

T obj = a;
T obj(a);

So you should be asking yourself, what is the difference between constructing a string object the following two ways

std::string{"Test"};
std::string("Test");

And the answer is that both the constructions above are the same and call the same constructor for std::string

For more on uniform initialization see https://softwareengineering.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax

CppCon 2016: std::accumulate: Exploring an Algorithmic Empire--Ben Deane

Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:

std::accumulate: Exploring an Algorithmic Empire

by Ben Deane

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

What is the most powerful algorithm in the STL? In the world? There are many cases to be made. But this talk explores what I think is a pretty good candidate, which C++ calls std::accumulate(). Tucked away in <numeric>, perhaps relatively unregarded when compared with workhorses like std::find_if() and std::partition(); nevertheless, std::accumulate() is in some sense the ur-algorithm on sequences.

Let’s explore the result of looking at code through an accumulate-shaped lens, how tweaking the algorithm for better composability can unlock many more uses, and how it can be further genericized with applications to parallelism, tree structures, and heterogeneous sequences.

std::accumulate(): it’s not just for adding things up!

Fuzzing beast with libFuzzer

A short blog post about my experience in fuzzing beast during my boost review

Fuzzing beast with libFuzzer

by Jens Weller

From the article:

During the weekend I wanted to take a closer look at beast, a http library proposed for boost. I planned to write an http client class, as thats something I'll need in some project later anyways. I've been looking at beast on and off for a few month now, and started by reviewing the documentation and examples to get a feel for the library it self.

Making things do stuff - Part 6 and 7--Glennan Carnie

The series continues.

Making things do stuff

by Glennan Carnie

Part 6Part 7

From the article:

As code designers we tend to eschew specific ‘stove-pipe’ code in favour of reusable code elements.  Up until now we’ve been coding some very specific examples so it’s probably worth looking at some more generic solutions...

Functors in C++-Part I and II--Mayank Jain

And how they are used by the std:

Functors in C++

by Mayank Jain

Part I - Part II

From the article:

Functor or function object is a C++ class which defines the operator ( ). Functor let’s you create objects which “looks like” functions...

CppCon 2016: Improving Performance Through Compiler Switches...--Tim Haines

Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:

Improving Performance Through Compiler Switches...

by Tim Haines

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Much attention has been given to what modern optimizing compilers can do with your code, but little is ever said as to how to make the compiler invoke these optimizations. Of course, the answer is compiler switches! But which ones are needed to generate the best code? How many switches does it take to get the best performance? How do different compilers compare when using the same set of switches? I explore all of these questions and more to shed light on the interplay between C++ compilers and modern hardware drawing on my work in high performance scientific computing.

Enabling modern optimizing compilers to exploit current-generation processor features is critical to success in this field. Yet, modernizing aging codebases to utilize these processor features is a daunting task that often results in non-portable code. Rather than relying on hand-tuned optimizations, I explore the ability of today's compilers to breathe new life into old code. In particular, I examine how industry-standard compilers like those from gcc, clang, and Intel perform when compiling operations common to scientific computing without any modifications to the source code. Specifically, I look at streaming data manipulations, reduction operations, compute-intensive loops, and selective array operations. By comparing the quality of the code generated and time to solution from these compilers with various optimization settings for several different C++ implementations, I am able to quantify the utility of each compiler switch in handling varying degrees of abstractions in C++ code. Finally, I measure the effects of these compiler settings on the up-and-coming industrial benchmark High Performance Conjugate Gradient that focuses more on the effects of the memory subsystem than current benchmarks like the traditional High Performance LinPACK suite.

Metaclasses for embedded domain specific languages -- Simon Brand

A post about an application of the new Metaclasses proposal.

Metaclasses for embedded domain specific languages

by Simon Brand

From the article:

Metaclasses are a proposed feature to C++ which will allow the creation of abstractions over classes and the extension of the language’s type definition system. Templates are a powerful host for other languages, and metaclasses only make them more so.

CppCast Episode 108: Teaching Concepts with Christopher Di Bella

Episode 108 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Christopher Di Bella to talk about his experience teaching C++ and his proposed changes to Concepts.

CppCast Episode 108: Teaching Concepts with Christopher Di Bella

by Rob Irving and Jason Turner

About the interviewee:

Christopher Di Bella will soon be a Runtime Technology Engineer at Codeplay, and was previously university tutor (teaching assistant) for the course 'Advanced C++ Programming', at the University of New South Wales, Australia. He is an avid C++ programmer, and also enjoys film, board games, and snowboarding in his spare time.

Modern C++ CI--Juan Medina

Complete set up of a project:

Modern C++ CI

by Juan Medina

From the article:

Modern C++ is great, some people are even calling it a new language, but is not only the language what is evolving the tool-chain is getter better, so doing continuous integration for cross platform projects is simple and effective.

I decide to do a simple project using some of the C++14 features and following the C++ Core Guidelines whenever its possible. The result is available in this repository.