Articles & Books

Quick Q: What can I do with a moved-from object? -- StackOverflow

Quick A: It's a valid object with an unspecified state, so start by using member functions that have no preconditions. For example, assign a new value to the object.

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient...

Effective C++11 update -- Scott Meyers

This week, Scott Meyers posted a couple of updates on how C++11 is coming to one of the world's most-loved C++ book series -- Effective C++.

 

First, here's Scott's preamble about his approach to Effective C++11:

Effective C++11: Background

I've mentioned in some earlier posts that I plan to start writing a new book, Effective C++11.  The purpose of this post is to tell you a little bit about it. Lest there be confusion, let me emphasize that there is no book yet. If everything falls into place the way I hope it will, there will be a book about 10 months from now. If. I'm not making any promises. [...]

 

As a followup, Scott then posted an early draft list of candidate Items for Effective C++11 as part of this post:

Effective C++11: Content and Status

[...] At last year's C++ and Beyond, I gave a talk entitled "Initial Thoughts on Effective C++11." It had my usual guideline format. I also gave a talk on "Secrets of the C++11 Threading API," which consisted of observations about C++11's threading support. The material in those talks, combined with the feedback I got from giving them and mixed in with my experience explaining the idea of universal references, ultimately yielded the initial list of candiate Items for EC++11. The current snapshot of my vision for Effective C++11 is: [...]

At least a million developers are looking forward to your book, Scott!

No pressure.

Quick Q: How to use std:: pointers to implement data structures (say, a DAG)? -- StackOverflow

This is a good specific example of a very common question about how to use smart pointers in data structures:

weak_ptr VS shared_ptr in graph node parent list

I have a directed acyclic graph implemented by Graph and Node classes. Each node has a list of pointers to children and a list of pointers to parents. [...] The Child list uses std::shared_ptr so that nodes are kept in memory at least as long as they have parents. But I don't want a node to own its parents, so I used weak_ptr for the pointers to parents.

But then there was a problem with the algorithms...

 

Quick Q: Can an enum class be converted to the underlying type? -- StackOverflow

Quick A: Yes, but it requires an explicit cast. One of the key differences between enum and enum class: the latter has no implicit conversion to the underlying type.

Can an enum class be converted to the underlying type?

Is there a way to convert an enum class field to the underlying type? I thought this would be automatic, but apparently not.

enum class my_fields : unsigned { field = 1 };

unsigned a = my_fields::field;

That assignment is being rejected by GCC. error: cannot convert 'my_fields' to 'unsigned int' in assignment.

Quick Q: Why do 'auto x = my_x;' and 'X& x = my_x;' declare different types? -- StackOverflow

Quick A: Remember that you can add things like const and & to an auto type...

'auto' and explicit variable declaration behaving differently

I have something like this:

[...]

auto bar1 = foo.GetBar();
auto bar2 = foo.GetBar(); //address of bar2 != address of bar1. why?

Bar& bar3 = foo.GetBar();
Bar& bar4 = foo.GetBar(); //address of bar3 == address of bar4.

What's going on here?

In a nutshell: To move or not to move -- Martin Moene

[Ed.: If you're new to C++11 and wondering about move semantics, but find the longer "introductory" articles about it a little too detailed, here's a nice nutshell discussion with a simple example and good links for further reading.]

An introductory discussion of the new C++11 rvalue references in connection with move semantics:

To Move or Not To Move

by Martin Moene

I'm probably not the only one who needs some time to become familiar with the new C++11 rvalue references. Recall std::auto_ptr to take care of a resource's lifetime. It could catch you by surprise and cause the loss of something valuable with its unconditional move behaviour.

 

Well that's exactly why things changed in C++11.

Here are a few excerpts that helped me to put the new rvalue references in perspective. ...

Continue reading...

Quick Q: Can a lambda function be recursive? -- StackOverflow

Quick answer: Yes, use std::function.

This recent short question shows a concise and correct answer, along with a link to a previous more complex example.

Can lambda functions be recursive?

Here is a plain old recursive function:

int fak(int n)
{
    return (n <= 1) ? 1 : n * fak(n - 1);
}

How would I write such a recursive function as a lambda function?

[](int n) { return (n <= 1) ? 1 : n * operator()(n - 1); }
// error: operator() not defined

[](int n) { return (n <= 1) ? 1 : n * (*this)(n - 1); }
// error: this wasn't captured for this lambda function

Is there any expression that denotes the current lambda so it can call itself recursively?

Quick Q: If I have a T&&, why would I write std::move? -- StackOverflow

Here's a common question about the relationship between && and std::move. It includes links to related variations of the question -- aka duplicates, though they're not always exact duplicates and it's helpful to see answers on different aspects of the same core question.

In C++11, why use std::move when you have &&?

I recently attended a C++11 seminar and the following tidbit of advice was given.

when you have && and you are unsure, you will almost always use std::move

Could any one explain to me why [when you already have a T&& -- Ed.] you should use std::move as opposed to some alternatives and some cases when you should not use std::move?

January standards papers mailing available

The between-meetings standards papers mailing is now available. It includes the spring meeting agenda, updated issues lists, and a number of new papers including at least one that came through the public std-proposals forum. (Update: Please also direct discussion about these papers to that forum.)

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup Disposition
SD-1 2013 PL22.16/WG21 document list Clark Nelson 2013-01-15 2013-01      
SD-2 ISO WG21 and INCITS PL22.16 membership list Clark Nelson 2013-01-11 2013-01      
SD-3 SC22/WG21 (C++) Study Group Organizational Information Herb Sutter 2012-10-04 2012-09      
SD-5 WG21 and PL22.16 (C++) Joint Mailing and Meeting Information Herb Sutter 2010-09-20 2012-09      
2013-01
N3493 Compile-time integer sequences Jonathan Wakely 2013-01-11 2013-01   Library Evolution  
N3494 A proposal to add special mathematical functions according to the ISO/IEC 80000-2:2009 standard Vincent Reverdy 2012-12-19 2013-01   Library Evolution  
N3495 inplace realloc Ariane van der Steldt 2012-12-07 2013-01   Library Evolution  
N3496 AGENDA, PL22.16 Meeting No. 60, WG21 Meeting No. 55, April 15-20, 2013 -- Bristol, UK Stephen D. Clamage 2013-01-02 2013-01      
N3497 Runtime-sized arrays with automatic storage duration (revision 4) Jens Maurer 2013-01-01 2013-01 N3467 Core  
N3498 Core Issue 1512: Pointer comparison vs qualification conversions (revision 2) Jens Maurer 2013-01-07 2013-01 N3478 Core  
N3499 Digit Separators Lawrence Crowl 2012-12-19 2013-01 N2281 Core  
N3500 New assert variants Olaf van der Spek 2012-11-28 2013-01   Library Evolution  
N3501 C++ Standard Core Language Active Issues, Revision 82 William M. Miller 2013-01-14 2013-01 N3480 Core  
N3502 C++ Standard Core Language Defect Reports and Accepted Issues, Revision 82 William M. Miller 2013-01-14 2013-01 N3481 Core  
N3503 C++ Standard Core Language Closed Issues, Revision 82 William M. Miller 2013-01-14 2013-01 N3482 Core  
N3505 Filesystem Library Proposal (Revision 4) Beman Dawes 2013-01-12 2013-01 N3399 Filesystem  
N3506 A printf-like Interface for the Streams Library Zhihao Yuan 2012-12-26 2013-01   Library Evolution  
N3507 A URI Library for C++ G. Matthews, D. Berris 2013-01-11 2013-01 N3484 Networking  
N3508 Any Library Proposal (Revision 2) B. Dawes, K. Henney 2013-01-11 2013-01 N3390 Library  
N3509 Operator Bool for Ranges Olaf van der Spek 2012-12-19 2013-01   Library Evolution  
N3510 std::split(): An algorithm for splitting strings Greg Miller 2013-01-10 2013-01 N3430 Library Evolution  
N3511 exchange() utility function Jeffrey Yasskin 2013-01-10 2013-01   Library Evolution  
N3512 string_ref: a non-owning reference to a string, revision 2 Jeffrey Yasskin 2013-01-11 2013-01 N3442 Library Evolution  
N3513 Range arguments for container constructors and methods, wording revision 2 Jeffrey Yasskin 2013-01-11 2013-01 N3456 Ranges  
N3514 A Proposal for the World's Dumbest Smart Pointer Walter Brown 2012-12-19 2013-01   Library Evolution  
N3515 Toward Opaque Typedefs for C++1Y Walter Brown 2013-01-11 2013-01   Evolution  
N3516 C++ Standard Library Active Issues List (Revision R81) Alisdair Meredith 2013-01-15 2013-01 N3473 Library  
N3517 C++ Standard Library Defect Report List (Revision R81) Alisdair Meredith 2013-01-15 2013-01 N3474 Library  
N3518 C++ Standard Library Closed Issues List (Revision R81) Alisdair Meredith 2013-01-15 2013-01 N3475 Library  
N3519 Feb 5, 2013 SG1 Teleconference Announcement and Agenda Hans Boehm 2013-01-11 2013-01   Concurrency  
N3520 Critical sections in vector loops Robert Geva 2013-01-11 2013-01   Concurrency  
N3521 convert() utility function Jeffrey Yasskin 2013-01-12 2013-01   Library Evolution