April 2016

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;
}

Sessions and object lifetimes--Andrzej Krzemieński

How will you move?

Sessions and object lifetimes

by Andrzej Krzemieński

From the article:

In this post we will see how C++ object lifetime can be used to control the duration of sessions: time spent owing and using a resource. The goal is to get a better understanding of what tools the language offers for using and sharing resources efficiently...

CppCast Episode 52: Macchina.io with Günter Obiltschnig

Episode 52 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Günter Obiltschnig to discuss the macchina.io library for IoT C++ development.

CppCast Episode 52: Macchina.io with Günter Obiltschnig

by Rob Irving and Jason Turner

About the interviewee:

Günter is the founder of the POCO C++ Libraries and macchina.io open source projects. He has been programming computers since age 12. In his career he has programmed everything from 8-bit home computers (C64, MSX) to IBM big iron systems (COBOL and JCL, VM/CMS and CICS), various Unix systems, OpenVMS, Windows NT in its various incarnations, the Mac (classic Mac OS and OS X), to embedded devices and iPhone/iPad. He has a diploma (MSc. equivalent) in Computer Science from the University of Linz, Austria.

His current main interests are embedded systems, cross-platform C++ development, JavaScript and, foremost, the Internet of Things. When not working, he spends time with his family or enjoys one of his hobbies — sailing, running, swimming, skiing, listening to or making music, and reading.