July 2013

Quick Q: How do you have an STL container of polymorphic objects? -- StackOverflow

Quick A: Try vector<unique_ptr<base>> by default.

What's the preferred C++ idiom to own a collection of polymorphic objects?

... I'm sure there's something related to move semantics in there (I could move the objects passed to add_items() to own them) but I still can't figure out how my collection would be declared.

What is the C++ idiom for this sort of polymorphic ownership (particularly with STL containers)?

When decltype meets auto -- Scott Meyers

A fresh Scott Meyers post about a brand-new C++14 feature:

When decltype meets auto

by Scott Meyers

... C++14 is on the horizon, and one of the new features sure to stifle even the strongest of yawns is the ability to declare types using decltype(auto). This feature leads to two questions, only the first of which is rhetorical:

  1. You can declare what?
  2. During type deduction for decltype(auto), which type deduction rules are to be followed: those for auto or those for decltype? Or does decltype(auto) have its own set of type deduction rules?

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?

Living with Lambdas -- Alfons Haffmans

living-with-lambdas.PNGWe have such a powerful language, and what a powerful language it is.

Living with Lambdas

by Alfons Haffmans

From the article:

You can work in the functional programming paradigm in C++. And you may be surprised at how complete C++’s support for functional programming is.

... C++ has always been a multi-paradigm language. And while previous attempts to add practical functional programming features required significant effort, recent additions to the C++ standard, like lambdas, have improved the situation. This paper explores the out-of-box support for functional programming provided by the new standard. We’ll look at techniques typically found in introductory functional programming textbooks. This article assumes familiarity with C++, but not necessarily with basic functional programming.

The source code is available in github and is compiled using gcc 4.8 installed on Mac OSX using MacPorts.

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

 

Modern C++ Programming with Test-Driven Development -- Jeff Langr

[Ed.: This upcoming book is currently in beta. Thanks Tom for pointing it out!]

 

langr-tdd.pngI have nothing to do with this book other than I've bought the beta book and am enjoying it thus far. It fills a gap in the market very nicely.

Modern C++ Programming with Test-Driven Development: Code Better, Sleep Better

by Jeff Langr, edited by Michael Swaine

If you program in C++ you’ve been neglected. Test-driven development (TDD) is a modern software development practice that can dramatically reduce the number of defects in systems, produce more maintainable code, and give you the confidence to change your software to meet changing needs. But C++ programmers have been ignored by those promoting TDD -- until now. In this book, Jeff Langr gives you hands-on lessons in the challenges and rewards of doing TDD in C++.

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.

Summer standards papers mailing available

Note: Some of these papers explore potential future standardization directions and alternatives. They do not discuss or propose to amend the C++14 Committee Draft paper whose ballot is currently in progress.

 

The official mid-meeting standards papers mailing is now available. If you are not currently a committee member, please direct discussion about these papers to the public std-proposals forum.

Note: Most or all of these papers have also been posted already to this site’s Standardization RSS feed.

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup Disposition
Every WD change mentioned in any paper in this mailing should be interpreted as a possible future direction, not as a change to the CD currently under ballot.
N3690 Programming Languages - C++ Stefanus Du Toit 2013-05-15 2013-07 N3691    
N3691 Working Draft, Standard for Programming Language C++ Stefanus Du Toit 2013-05-16 2013-07 N3485    
N3692 C++ Editor's Report, October 2012 Stefanus Du Toit 2013-05-16 2013-07      
N3693 Working Draft, Technical Specification -- File System Beman Dawes 2013-06-28 2013-07 N3505 Filesystem  
N3694 Feature-testing recommendations for C++ Clark Nelson 2013-06-27 2013-07   Feature test  
N3695 SG5: Transactional Memory (TM) Meeting Minutes 2013/03/11-2013/06/10 Michael Wong 2013-06-28 2013-07      
N3696 Proposal to extend atomic with priority update functions Bronek Kozicki 2013-06-26 2013-07   Concurrency  
N3697 Business Plan and Convener's Report Herb Sutter 2013-06-25 2013-07      
N3698 July 25-26 Santa Clara SG1 Meeting Announcement and Agenda Hans-J. Boehm 2013-06-30 2013-07      
N3699 A proposal to add a generalized callable negator Tomasz Kamiński 2013-05-27 2013-07   Library  
N3700 Hierarchical Data Structures and Related Concepts for the C++ Standard Library B. Reiter, R. Rivera 2013-06-22 2013-07 N2101 Library  
N3701 Concepts Lite A. Sutton, B. Stroustrup, G. Dos Reis 2013-06-28 2013-07 N3580 Concepts  
N3702 Introducing an optional parameter for mem_fn, which allows to bind an object to its member function Mikhail Semenov 2013-06-28 2013-07   Library  
N3703 Extending std::search to use Additional Searching Algorithms (Version 3) Marshall Clow 2013-06-28 2013-07 N3606 Library  

New paper: N3703, Extending std::search to use Additional Searching Algorithms (v3) -- Marshall Clow

Note: This paper explores potential future standardization directions and alternatives. It does not discuss or propose to amend the C++14 Committee Draft paper whose ballot is currently in progress.

 

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: N3703

Date: 2013-06-28

Extending std::search to use Additional Searching Algorithms (Version 3)

by Marshall Clow

Excerpt:

Note:

  • This is an update of n3606, presented in Portland. The major differences are support for comparison predicates in Boyer-Moore and Boyer-Moore-Horspool, and wording for the standard.
  • n3606 was an update of n3411, presented in Portland. The major difference is a different interface for the search functions.

[...]

std::search is a powerful tool for searching sequences, but there are lots of other search algorithms in the literature. For specialized tasks, some of them perform significantly better than std::search. In general, they do this by precomputing statistics about the pattern to be searched for, betting that this time can be made up during the search.

The basic principle is to break the search operation into two parts; the first part creates a "search object", which is specific to the pattern being searched for, and then the search object is passed, along with the data being searched, to std::search.

This is done by adding an additional overload to std::search, and some additional functions to create the search objects. ...