March 2015

Quick Q: How to write variadic template constructor?

Quick A: use an std::initializer_list.

Recently on SO:

Writting variadic template constructor

Recently I asked this question but now I would like to expand it. I wrote the following class:

template <class T>
class X{
public:
    vector<T> v;
    template <class T>
    X(T n) {
        v.push_back(n);
    }
    template <class T, class... T2>
    X(T n, T2... rest) {
        v.push_back(n);
        X(rest...);
    }
};

When creating and object using

X<int> obj(1, 2, 3);  // obj.v containts only 1

Vector only contains the first value, but not others. I've checked and saw that constructor is called 3 times, so I'm probably creating temp objects and filling their vectors with the rest of the arguments. How do I solve this problem?

Quick Q:How to modify a tuple in a vector of tuples c++?

Quick A: Capture the tuple by reference.

Recently on SO:

Modifying a tuple in a vector of tuples c++

I have a vector of tuples vector<tuple<int,int>> vector; and I want to modify one of the tuples it contains.

for (std::tuple<int, int> tup : std::vector)
{
    if (get<0>(tup) == k)
    {
        /* change get<1>(tup) to a new value
         * and have that change shown in the vector
         */
    }
}

I am unsure how to change the value of the tuple and have the change be reflected in the vector. I have tried using

get<1>(tup) = v;

but that doesn't change the value of the tuple that is in the vector. How can I do this? Thanks.

How to write a standard-like algorithm -- Indi

Explicit C++ has posted a nice tutorial on how to implement an algorithm in C++.

How to write a standard-like algorithm

by Indi

from the article:

Writing a standard-like algorithm should be one of the key parts of a modern C++ beginner’s course outline. This post will be a whirlwind guide through the steps toward creating a standard-like algorithm. The focus is not on the algorithm itself, but on the process of creating one.

CppCon 2014 ...Scaling Visualization in concurrent C++ programs--Fedor G Pikus

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

...Scaling Visualization in concurrent C++ programs

by Fedor G Pikus

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

High performance is one of the main reasons programmers choose C++ for their applications. If you are writing in C++, odds are you need every bit of computing power your hardware can provide. Today, this means writing multi-threaded programs to effectively utilize the multiple CPU cores that the hardware manufacturers keep adding. Everyone knows that writing multi-threaded programs is hard. Writing correct multi-threaded programs is even harder. Only after spending countless hours debugging race conditions and weird intermittent bugs do many programmers learn that writing efficient multi-threaded programs is harder yet. Have you ever wanted to see what are all your threads doing when they should be busy computing? This talk will show you how.

We begin by studying several techniques necessary for collecting large amounts of data from the running program very efficiently, with little overhead and minimal disruption to the program itself. We will cover efficient thread-safe memory management and efficient thread-safe disk I/O. Along the way we dabble in lock-free programming just enough to meet our needs, lest the subject will spiral into an hour-long talk of its own. With all these techniques put together, we can collect information about what each thread is doing, which threads are computing and what exactly, and which threads are slacking off waiting on locks, and do it at the time scale of tens of microseconds if necessary. Then we process the collected data and create a timeline that shows exactly what the program was doing at every moment in time.

Iterators++, Part 3 -- Eric Niebler

Eric Niebler concludes his series about proxy iterators with:

Iterators++, Part 3

by Eric Niebler

From the article:

This is the forth and final post in a series about proxy iterators, the limitations of the existing STL iterator concept hierarchy, and what could be done about it. The first three posts describe the problems of proxy iterators, the way to swap and move their elements, and how to rigorously define what an Iterator is.

This time around I’ll be focusing on the final problem: how to properly constrain the higher-order algorithms so that they work with proxy iterators.

N4388: A Proposal to Add a Const-Propagating Wrapper to the Standard Library -- J Coe, R Mill

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

Document number: N4388

Date: 2015-02-25

A Proposal to Add a Const-Propagating Wrapper to the Standard Library

by Jonathan Coe, Robert Mill

Excerpt:

We propose the introduction of a propagate_const wrapper class that propagates const-ness to pointer-like member variables. ... The behaviour of const member functions on objects with pointer-like data members is seen to be surprising by many experienced C++ developers. A const member function can call non-const functions on pointer-like data members and will do so by default without use of const_cast...

A conditional copy constructor -- Andrzej KrzemieĊ„ski

Andrzej writes in his recent blog about an issue library writers have to take care of.

A conditional copy constructor

by Andrzej Krzemieński

From the article:

In this post we will try to define a ‘wrapper’ class template that does or does not have a copy constructor depending on whether the wrapped class has it. This will be a good opportunity to explore in depth a couple of advanced C++ features. Note that this is a rather advanced topic and, unless you are writing or maintaining a generic library, you will probably never need this knowledge.

CppCon 2014 Meta Techniques: Heterogeneous Polymorphism&Fast Prototyping at Facebook--Marcelo Juchem

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

Meta Techniques: Heterogeneous Polymorphism & Fast Prototyping at Facebook

by Marcelo Juchem

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

As data driven systems evolve there's an ever growing demand for bringing new functionality into existing systems in an efficient, maintainable and least intrusive manner. When implementing features with different semantics or interfaces, virtual inheritance requires a compromise between design simplicity and performance. This implies a need for new techniques to achieve heterogeneous polymorphism efficiently. With C++11 and 14, type lists, type maps and variants can now be trivially implemented by the initiated. Facebook moves fast so we quickly adopted the new standards to further explore the capabilities of the type system. This talk demonstrates some meta-programming techniques like reflection and compile-time built structures to achieve heterogeneous polymorphism and fast prototyping.

C++ User Group Meetings in March

The monthly overview on the upcoming C++ User Group Meetings:

C++ User Group Meetings in March

by Jens Weller

The list of this month meetings:

4.3 C++ UG Saint Louis - DD Part 3 - C++11/14 Standard Libraries
5.3 C++ UG Paris - C++ FRUG #6 - La métaprogrammation, non non ca sert en vrai
11.3 C++ UG Utah - Graphics and Audio with Cinder
11.3 C++ UG Hungary - More Effective STL
11.3 C++ UG San Francisco/ Bay area - Experience with C++11 in ArangoDB
12.3 C++ UG NRW/Aachen - C++ User Gruppe (März)
12.3 C++ UG Dresden - Coding Dojo
12.3 C++ UG New York - How to Write A Shared Library
16.3 C++ UG Denver - C++ Lightning Talks
16.3 C++ UG Austin - North Austin Monthly C/C++ Pub Social
17.3 C++ UG Chicago - Richard Stallman
17.3 C++ UG Edinburgh - C++ Edinburgh
17.3 C++ UG Montpellier - Rencontre C++ mars
17.3 C++ UG Berlin - monthly meeting
18.3 C++ UG Bristol - Save the date
18.3 C++ UG Düsseldorf - Treffen der C++ User Gruppe NRW
18.3 C++ UG Ann Arbor - Meet & Greet
18.3 C++ UG Hamburg - C++ in der numerischen Programmierung
19.3 C++ UG Ruhrgebiet - surprise, surprise
24.3 C++ UG Portland - PDXCPP March Meeting
25.3 C++ UG San Francisco/ Bay area - Workshop and Discussion Group
26.3 C++ UG Bremen - First meetup
26.3 C++ UG Rhein-Neckar - C++ Usergroup Meeting
26.3 C++ UG Munich - Monthly Meeting
26.3 C++ UG Madrid - Sistemas Distribuidos con ZMQ y Google Protocol Buffers
27.3 C++ UG Istanbul - Smart Pointers - from 03 to 17