April 2016

CppCast Episode 53: Boost Hana with Louis Dionne

Episode 53 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason by Louis Dionne to discuss C++ metaprogramming with Boost Hana.

CppCast Episode 53: Boost Hana with Louis Dionne

by Rob Irving and Jason Turner

About the interviewee:

Louis is a math and computer science enthusiast with interest in C++ (meta)programming, functional programming, domain specific languages and related subjects. He is an active member of the Boost community, and recently wrote the Boost.Hana metaprogramming library.

The Ultimate Question of Programming, Refactoring, and Everything

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort.

The Ultimate Question of Programming, Refactoring, and Everything

by Andrey Karpov

From the article:

The scope of my interests − the C/C++ language and the promotion of code analysis methodology. I have been Microsoft MVP in Visual C++ for 5 years. The main aim of my articles and work in general - is to make the code of programs safer and more secure. I'll be really glad if these recommendations help you write better code, and avoid typical errors. Those who write code standards for companies may also find some helpful information here.

C++ meetup in Madrid, Spain: C++ in Fintech

Today we have another C++ meetup in Madrid, Spain.

C++ in Fintech

C++ applied to fintech products and services

What to expect at the meetup:

Short keynotes on how C++ is applied to risk calcuation apps, fast market information transfers, quantitative finance and much more. It will be hosted at Indizen HQs and we will have programers from BME, BBVA and Indizen.

Overload 132 is now available

ACCU’s Overload journal of April 2016 is out. It contains the following C++ related articles.

Overload 132

From the journal:

The Tao of Scratch
Scratch is an environment designed to help young people learn to code. Patrick Martin walks us through it. by Patrick Martin

Knowledge-Sharing Architects As An Alternative to Coding Architects
Should architects write code? Sergy Ignatchenko explores this controversial subject. by Sergey Ignatchenko

QM Bites: Understand Windows OS Identification Preprocessor Macros
There’s confusion between user-defined and predefined Windows 32/64-bit operating-system identification macros. Matthew Wilson shines light on the issue. by Matthew Wilson

Why Collaboration is Key for QA Teams in an Agile World
Agile processes can have an impact on QA departments. Greg Law considers how they can adapt to survive and even thrive. by Greg Law

How to Diffuse Your Way Out of a Paper Bag
Diffusion models can be used in many areas. Frances Buontempo applies them to paper bag escapology. by Frances Buontempo

Stufftar
How do you quickly transfer data from one machine to another? Ian Bruntlett shows us the bash script he uses. by Ian Bruntlett

QM Bites: looping for-ever
Never-ending loop constructs can confound user and compiler in subtle ways. Matthew Wilson offers advice to maximise portability and transparency.

Using Enum Classes as Bitfields
Scope enums have many advantages over standard enums. Anthony Williams shows how to use them as bitmasks. by Anthony Williams

9.7 Things Every Programmer Really, Really Should Know
Most of us have heard of the twelve step program. Teedy Deigh introduces a 9.7 step plan for programmers.

N4590: PL22.16/WG21 draft agenda: 20-25 Jun 2016, Oulu, FI -- Clark Nelson

A new WG21 paper is available. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N4590

Date: 2016-04-07

PL22.16/WG21 draft agenda: 20-25 Jun 2016, Oulu, FI

by Clark Nelson

Excerpt:

Meeting objectives The primary goals of this meeting will be:

  • Approve a CD for C++17
  • Advance Library Fundamentals v2 toward TS
  • Advance Networking toward PDTS
  • Advance Parallelism v2 toward PDTS
  • Advance Ranges toward PDTS

Quick Q: Is Stephen Lavavej's Mallocator the same in C++11?

Quick A: No, it has changed.

Recently on SO:

Is Stephen Lavavej's Mallocator the same in C++11?

STL himself has an answer to this question in his STL Features and Implementation techniques talk at CppCon 2014 (Starting at 26'30).

The slides are on github.

I merged the content of slides 28 and 29 below:

#include <stdlib.h> // size_t, malloc, free
#include <new> // bad_alloc, bad_array_new_length
template <class T> struct Mallocator {
  typedef T value_type;
  Mallocator() noexcept { } // default ctor not required
  template <class U> Mallocator(const Mallocator<U>&) noexcept { }
  template <class U> bool operator==(
    const Mallocator<U>&) const noexcept { return true; }
  template <class U> bool operator!=(
    const Mallocator<U>&) const noexcept { return false; }

  T * allocate(const size_t n) const {
      if (n == 0) { return nullptr; }
      if (n > static_cast<size_t>(-1) / sizeof(T)) {
          throw std::bad_array_new_length();
      }
      void * const pv = malloc(n * sizeof(T));
      if (!pv) { throw std::bad_alloc(); }
      return static_cast<T *>(pv);
  }
  void deallocate(T * const p, size_t) const noexcept {
      free(p);
  }
};

Note that it handles correctly the possible overflow in allocate.

Course: Boost Your Productivity with Modern C++ -- Peter Gottschling

peter-gottschling.jpgA repeat of last year's popular course, again being held in English and German:

Boost Your Productivity with Modern C++

by Peter Gottschling

Abstract:

Templates (generic programming)

  • Function templates
  • Class templates
  • Variadic Templates (C++11)
  • Concepts
  • Specialization
  • Template arguments that are not types
  • Functors
  • Lambda functions (C++11)

Standard Template Library

  • Iterator concept
  • Containers
  • Functions
  • Meta-programming
  • Let the compiler compute
  • Providing type informations
  • Auto and decltype (C++11)
  • Const-adaptive classes
  • Expression templates

Other advanced and new techniques

  • Calling functions from derived classes without overhead
  • RValues and move semantics (C++11)
  • Initialization lists (C++11)
  • New for-loops (C++11)

Peter Gottschling is author of the Matrix Template Library 4, co-author of the Boost Graph Library and other scientific libraries. He is vice-chair of DIN's programming language group and head of the German delegation in the ISO committee for C++ standardization. He is managing director of SimuNova and taught C++ at TU Dresden, TU Berlin and Indiana University.

Quick Q:Is it possible in C++ to iterate over a std::map with unpacked key and value?

Quick A: An easy solution is not supported by the standard, it may come later.

Recently on SO:

Is it possible in C++ to do std::map<> “for element : container” iteration with named variables (eg, key and value) instead of .first and .second?

You could write a class template:

template <class K, class T>
struct MapElem {
    K const& key;
    T& value;

    MapElem(std::pair<K const, T>& pair)
        : key(pair.first)
        , value(pair.second)
    { }
};

with the advantage of being able to write key and value but with the disadvantage of having to specify the types:

for ( MapElem<int, std::string> kv : my_map ){
    std::cout << kv.key << " --> " << kv.value;
}

And that won't work if my_map were const either. You'd have to do something like:

template <class K, class T>
struct MapElem {
    K const& key;
    T& value;

    MapElem(std::pair<K const, T>& pair)
        : key(pair.first)
        , value(pair.second)
    { }

    MapElem(const std::pair<K const, std::remove_const_t<T>>& pair)
        : key(pair.first)
        , value(pair.second)
    { }
};

for ( MapElem<int, const std::string> kv : my_map ){
    std::cout << kv.key << " --> " << kv.value;
}

It's a mess. Best thing for now is to just get used to writing .first and .second and hope that the structured bindings proposal passes, which would allow for what you really want:

for (auto&& [key, value] : my_map) {
    std::cout << key << " --> " << value;
}