Articles & Books

Why Would You Ever Pass a Container By Value? -- Andrew Koenig

From the desk of ARK:

Why Would You Ever Pass a Container By Value?

by Andrew Koenig

From the article:

Consider two fundamental features of C++: functions and references. Which shall we teach first?

If we teach references first, there is the problem of coming up with interesting example programs that use references but completely avoid user-defined functions. This is hard to do because the most common use of references is as function parameters — so it's probably easier to teach functions first.

However, if we teach functions before we teach references, then every function we write must accept its arguments by value — references not yet being available as an alternative...

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 )) // ...

[...]

Quick Q: Why do I have to std::move a variable that is already a &&? -- StackOverflow

Today on SO, a frequently asked question and a nicely summed up answer:

std::move on a variable which already is T&&

On the page http://msdn.microsoft.com/en-us/library/dd293665.aspx Microsoft has an example on how to write a move constructor. It is essentially of the form:

MyClass::MyClass(MyClass&& lhs)
{
    *this = std::move(lhs);
}

I have tried and std::move really is required here, but why? I thought the only thing move did was to convert to T&&. But lhs is already of type MyClass&&, is it not?

Some Notes on C++11 Lambda Functions -- Ranju V.

ranjuv.jpgHere's a nice lambda synopsis with some usage notes.

Some notes on C++11 lambda functions

by Ranju V.

From the article:

Bjarne Stroustrup says that C++11, which is the latest ratified revision of the C++ standard, “feels like a new language”.  I think lambda functions are a big part of what makes the language feel so very different from C++03.  Lambda functions basically allow you to do things like this:

vector<int> nums { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto evens = count_if(begin(nums), end(nums), [](int num) {
    return (num % 2) == 0;
});

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...

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.