intermediate

GotW #94 Solution: AAA Style (Almost Always Auto) -- Herb Sutter

The solution to the latest GotW problem is now available:

GotW #94 Solution: AAA Style (Almost Always Auto)

by Herb Sutter

From the article:

4. When declaring a new local variable x, what advantages are there to declaring it using auto and one of the two following syntaxes:

(a) auto x = init; when you don’t need to commit to a specific type? (Note: The expression init might include calling a helper that performs partial type adjustment, such as as_signed, while still not committing to a specific type.)

(b) auto x = type{ init }; when you do want to commit to a specific type by naming a type?

Quick Q: Why do I have to write 'mutable' on a lambda? -- StackOverflow

Inquiring minds want to know:

Why does C++0x's lambda require “mutable” keyword for capture-by-value, by default?

Short example:

#include <iostream>

int main()
{
    int n;
    [&](){n = 10;}();             // OK
    [=]() mutable {n = 20;}();    // OK
    // [=](){n = 10;}();          // Error: a by-value capture cannot be modified in a non-mutable lambda
    std::cout << n << "\n";       // "10"
}

The question: Why do we need the mutable keyword? It's quite different from traditional parameter passing to named functions. What's the rationale behind?

I was under the impression that the whole point of capture-by-value is to allow the user to change the temporary -- otherwise I'm almost always better off using capture-by-reference, aren't I?

Any enlightenments?

(I'm using MSVC2010 by the way. AFAIK this should be standard)

Quick Q: Where do I have to write 'template' and 'typename'? -- StackOverflow

This question is an oldie but goodie. Note that in C++11 some of this becomes clearer, because it's now recommended to use constexpr and using template aliases instead of "traits" styles, and traits are one of the bigger (but not only) reasons to have a template refer to another template.

Where and why do I have to put the “template” and “typename” keywords?

In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: ...

Contextually Converted to Bool -- Chris Sharpe

A nice writeup of when this "it just works the way you expect" feature kicks in:

Contextually converted to bool

by Chris Sharpe

From the article:

Something I found mildly surprising about C++11 is that this works:

#include <iostream>

struct Testable
{
    explicit operator bool() const { return true; }
};

int main()
{
    Testable t;
    if (t)
        std::cout << "Converted to true!\n";
}

That is, it compiles and prints Converted to true!.

The new bit here is the explicit keyword. When I first saw an example like this, I expected to have to write

if (bool( t )) // ...

[...]

Boost Your Productivity with C++11 and Templates (Sep 9-13, Oct 7-11) -- Peter Gottschling

peter-gottschling.jpgThis course is being held in English and German:

Boost Your Productivity with C++11 and Templates

by Peter Gottschling

  • September 9-13 (German)
  • October 7-11 (English)

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.

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.

C++11/14 Training Materials -- Scott Meyers

meyers-newcppmaterials.PNGC++14 is still very much a draft -- it only became feature-complete in April and is now in its primary comment ballot. But interest in this new standard is high, with a restarted "Guru of the Week" series focusing on C++14 and now Scott Meyers has produced the first book-like materials that include significant coverage of C++14.

C++11 Training Materials Updated -- Now With C++14 Info!

For the seventh time since originally releasing them over three years ago, I've updated my annotated training materials for "The New C++". Until this update, "the new C++" referred to C++11, but with this revision, I'm including treatment of several features from draft C++14 that I believe will make it into the new new standard. As far as I know, this makes my training materials the first "book-like" publication that covers features in C++14.