basics

C++11 New Feature: auto Type Deduction -- FangLu

A new article on auto type deduction has been posted to IBM's C/C++ cafe.

C++11 new feature: auto type deduction

by FangLu

This article describes a new C++11 feature that deprecates the auto keyword as a storage class specifier, but reserves auto as a type specifier that directs the compiler to deduce the type of a declared variable from its initialization expression. This auto type deduction feature can increase programming convenience, eliminate potential typing errors, and simplify the code.

Quick Q: What's the weak_ptr equivalent for unique_ptr? -- StackOverflow

Quick A: There isn't, because unique_ptr is unique. If you want shared ownership or shared observation, you need a shared_ptr. The "shared"-ness in shared_ptr enables both shared ownership (shared_ptr) and shared observation (weak_ptr).

shared_ptr<> is to weak_ptr<> as unique_ptr<> is to... what?

In C++11, you can use a shared_ptr<> to establish an ownership relation with an object or variable and weak_ptr<> to safely reference that object in a non-owned way.

You can also use unique_ptr<> to establish an ownership relation with an object or variable. But what if other, non-owning objects want to also reference that object? weak_ptr<> isn't helpful in this case. Raw pointers are helpful but bring various downsides (e.g. they can be automatically initialized to nullptr but this is accomplished through techniques that are not consistent with the std::*_ptr<> types).

What is the equivalent of weak_ptr<> for non-owning references to objects owned via unique_ptr<>?

Working with Dynamic Memory in C++ -- Stan Lippman, Josée Lajoie, Barbara Moo

A chapter worth reading on InformIT, excerpted from C++ Primer 5th Edition:

Working with Dynamic Memory in C++

by Stan Lippman, Josée Lajoie, Barbara Moo

C++ lets you allocate objects dynamically. The authors of C++ Primer discuss why properly freeing dynamic memory is a rich source of bugs, and how the new library defines smart pointers -- shared_ptr, unique_ptr, and weak_ptr -- that make managing dynamic memory much safer.

Quick Q: How do I remove a unique_ptr from a queue? -- StackOverflow

Quick A: move(q.front()).

remove unique_ptr from queue

I'm trying to figure out how/if I can use unique_ptr in a queue.

// create queue
std::queue<std::unique_ptr<int>> q;

// add element
std::unique_ptr<int> p (new int{123});
q.push(std::move(p));

// try to grab the element
auto p2 = foo_queue.front();
q.pop();

I do understand why the code above doesn't work. Since the front & pop are 2 separate steps, the element cannot be moved. Is there a way to do this?

Quick Q: What are move semantics? -- StackOverflow

One of the highest-ranked C++ questions (and answers) on StackOverflow is worth re-reading if you're new to the topic:

What are move semantics?

I just finished listening to the Software Engineering talk radio podcast interview with Scott Meyers regarding C++0x. Most of the new features made sense to me, and I am actually excited about C++0x now, with the exception of one. I still don't get move semantics... What are they exactly?

Moving an Object Does Not Destroy The Original -- Andrew Koenig

Andy's next on move semantics:

Moving an Object Does Not Destroy The Original

by Andrew Koenig

From the article:

Recall first that objects are generally moved instead of copied because the originals are about to go away. When I learned this fact, I thought at first that moving an object should destroy the original. ...

I was surprised to learn that that is not what happens. Instead, whenever a program moves an object, it must leave that object with a valid value so that it can be destroyed later. ...

 

Auto: A Necessary Evil? -- Roger Orr

Roger Orr has written about the history and use of auto in ACCU's Overload 115:

Auto -- A Necessary Evil?

by Roger Orr

From the article:

The keyword auto now lets you declare variables where the compiler provides the actual type and the programmer is either unwilling or unable to name the actual type. The keyword can also be used in function definitions to let you provide the return type after the rest of the function declaration, which is useful when the return type depends on the type of the arguments.

As with any new keyword there are questions about usage –- at two levels. First of all, where and how are programmers permitted to use the new feature. Secondly, what guidance is there to sensible adoption of the new feature. I intend to start with by answering the first question and then subsequently focus on the second.

Explicit Is Better Than Implicit -- K-ballo

tales-of-cpp.PNGTales of C++, Episode 5:

Explicit Is Better Than Implicit

The Zen of Python tell us that Explicit is better than Implicit. This is good advice for any and all languages, but what are the implications in the C++ lands? There is one particular C++ feature that relates directly to that advice; one important enough that grants on its own the introduction of a keyword. That feature is user-defined conversions and that keyword is explicit.

GotW #93 Solution: Auto Variables, Part 2 -- Herb Sutter

The solution to the latest GotW problem is now available:

GotW #93 Solution: Auto Variables, Part 2

by Herb Sutter

From the article:

As you worked through these cases, perhaps you noticed a pattern: The cases are mostly very different, but what they have in common is that they illustrate reason after reason motivating why (and how) to use auto to declare variables.

Let’s dig in and see...