July 2013

Expression Templates -- Volker Krause

volker-krause.jpgFrom this month's KDE Akademy conference:

Expression Templates: How I Learned to Stop Worrying and Love Template Meta-Programming (30 min video) (slides)

by Volker Krause

The speaker's opening words should be enough to get you to set aside a half hour:

"I'm going to talk about template metaprogramming in general and expression templates in particular. And that's obviously something we use every day. <laughter> You laugh, but in half an hour you will see that we actually do..."

 

Abstract:

Contrary to common perception, expression templates are not bizarre meta-programming theory but something you are (possibly unknowingly) using every day. We'll see what they are good for, how they work and and what to do if they don't.

Originally invented by Todd Veldhuizen for the use in the math library Blitz++, expression templates are often seen as one of those esoteric applications of C++ template meta-programming that results in completely incomprehensible code that only Marc and Thiago understand at best, and that gives you compiler errors longer than an average discussion on the right display manager.

In this talk we will try to counter this (wrong) perception, by looking at why and how this (syntactically) indeed somewhat complex code results in very elegant API and often outperforms conventional implementations. On the way we will see that C++ template meta-programming code is actually not that scary, and that template-related compiler errors aren't that bad either.

Now why would this be of interest to the average Qt or KDE developer? Well, since the introduction of QStringBuilder in Qt 4.7 we have expression templates for a very common use case, string concatenations. And if your code is somewhere in KDE Git, it's very likely Laurent enabled the use of QStringBuilder for you already. So, we are not talking about exotic theory here, but about tools you are using every day.

Is Moving Objects Worth the Hassle? -- Andrew Koenig

Koenig's latest just went live at DDJ:

Is Moving Objects Worth the Hassle?

by Andrew Koenig

From the article:

Last week, I discussed how C++ compilers use overloading to decide whether to move or copy an object. This week, I'd like to take a step back and discuss why moving instead of copying is worth doing in the first place.

You might think that this claim needs no justification. After all, copying takes time; so programs that copy a lot of data will be slower than programs that avoid doing so. However, this kind of unthinking optimization can lead to trouble. For example...

New paper: N3705, Agenda and Meeting Notice for WG21 Telecon Meeting -- Herb Sutter

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

 

Document number: N3705

Date: 2013-07-23

Agenda and Meeting Notice for WG21 Telecon Meeting

by Herb Sutter

Excerpt:

Note: As discussed at the previous telecon and in committee email, this telecon is being held one week before the face-to-face meeting, rather than two weeks, to avoid holidays and other conflicts.

The Universal Reference/Overloading Collision Conundrum -- Scott Meyers

meyers-nwcpp-13.PNG[Blog suggestion by NoSenseEtAl. Text below added by editor.]

Scott Meyers gave a well-received "beta" talk last week at NWCPP on the topic below, which he described as the most complex Item he's encountered as he prepares his upcoming book Effective C++11/14:

The Universal Reference/Overloading Collision Conundrum

by Scott Meyers

Abstract:

To help address the confusion that arises when rvalue references become lvalue references through reference collapsing, Scott Meyers introduced the notion of "universal references." In this presentation, he builds on this foundation by explaining that overloading functions on rvalue references is sensible and useful, while seemingly similar overloading on universal references yields confusing, unhelpful behavior. But what do you do when you want to write a perfect forwarding function (which requires universal references), yet you want to customize its behavior for certain types? If overloading is off the table, what's on? In this talk, Scott surveys a variety of options.

Though Scott will give a one-slide overview of the idea behind universal references at the beginning of the presentation, attendees are encouraged to familiarize themselves with the notion in more detail prior to the talk. Links to written and video introductions to universal references are available here.

How do you write a universal memoization function in C++? -- StackOverflow

From Python to C++:

Writing Universal memoization function in C++

Looking for a way to implement a universal generic memoization function which will take a function and return the memoized version of the same?

Looking for something like @memo (from Norving's site) decorator in python.

def memo(f):
    table = {}
    def fmemo(*args):
        if args not in table:
            table[args] = f(*args)
        return table[args]
    fmemo.memo = table
    return fmemo

Going more general, is there a way to express decorators in C++?

The Point of No Return -- bulldozer00

A cute nugget about [[noreturn]]:

The Point of No Return

by bulldozer00

As part of learning the new feature set in C++11, I stumbled upon the weird syntax for the new “attribute” feature: [[ ]]. One of these new C++11 attributes is [[noreturn]]. ...

Quick Q: When should you use constexpr? -- StackOverflow

Quick A: When you want to potentially evaluate a calculation at compile time. You can do much, much more than just "return 5;".

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there?

// preprocessor.
#define MEANING_OF_LIFE 42
// constants:
const int MeaningOfLife = 42;
// constexpr-function:
constexpr int MeaningOfLife () { return 42; }

It seems to me that if I wrote a function that return a literal value, and I came up to a code-review, someone would tell me, I should then, declare a constant value instead of writing return 5.

C++'s Best Feature -- Andrzej KrzemieĊ„ski

Andrzej's title is not only catchy, but completely correct (we politely disagree with your final disclaimer, sorry Andrzej): Deterministic lifetime with destructors is C++'s best feature, and very underappreciated. Like most wonderful things, you appreciate it most when it's gone, namely when you're using another language where it's absent.

C++'s best feature

by Andrzej Krzemieński

From the article:

C++, if you want to learn all of it, is big, difficult and tricky. If you look at what some people do with it, you might get scared. New features are being added. It takes years to learn every corner of the language.

But you do not need to learn all of it. Effective use of C++ requires only the knowledge of a couple of its essential features. In this post, I am going to write about one C++ feature that I consider one of the most important. The one that makes me choose C++ rather than other popular programming languages. ...

New paper: N3704, September Meeting Agenda -- Stephen D. Clamage

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N3704

Date: 2013-07-13

Agenda for PL22.16 Meeting No. 61, WG21 Meeting No. 56, September 23-28, 2013 -- Chicago, IL, USA

by Stephen D. Clamage

Excerpt:

The primary focus of this meeting will be CD ballot resolution for C++ 2014.

Remaining time will be devoted to processing DRs and issues from issue lists, as well as work on
proposed new features for future standards. Each working group and study group sets its own detailed
agenda.

Sometimes You Must Violate an Abstraction to Maintain It -- Andrew Koenig

Koenig explains std::move as, well, just a bit of a fib, really:

Sometimes You Must Violate an Abstraction to Maintain It

by Andrew Koenig

From the article:

What std::move really does is to return its argument as an rvalue reference. In effect, every time we use std::move, we are telling a lie. In this case, by writing std::move(t.s), we are saying that we want to use t.s, but to do so in a way that treats t.s as an rvalue. It is acceptable for us to tell this lie for exactly the same reason that it is acceptable for us to cast t.s to string&& in the previous example: We know that t.s is a member of t, and t really refers to an rvalue in our caller's context.

We can tell such lies any time we are willing to take responsibility for the consequences. ...