basics

Quick Q: Is the safe-bool idiom obsolete in C++11? -- StackOverflow

Quick A: Yes. Another way that modern C++ is safer and simpler.

(If you don't know what the safe-bool idiom is, don't worry. It's  a workaround that's now obsolete.)

Xeo asked:

Is the safe-bool idiom obsolete in C++11?

This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple

explicit operator bool() const;

... Is our assumption in the title correct? I hope we didn't overlook any potential drawbacks.

Preconditions, Part 2 -- Andrzej Krzemieński

Andrzej continues this month with more interesting thoughts on preconditions.

Preconditions, Part 2

by Andrzej Krzemieński

In this post I will continue sharing my thoughts on preconditions. It will cover some philosophy behind the concept of preconditions (and bugs), and investigate the possibility of employing the compiler to verify some preconditions. Many people provided a useful feedback on my previous post. I will also try to incorporate it into this post.

Note that this article diverges from recommended practice in one way... it hints at the idea of throwing exceptions to report precondition violations. Instead, per C++ Coding Standards and other established guidance, prefer to use assertions to check preconditions: precondition violations are just bugs in the caller's code that should be caught at test time, assertions cause no overhead in production, and assertions fire immediately at the line of code that contains the bug without losing the call stack and other local context. Using assertions is still considered to be a best practice.

Learning Modern C++: An Interview with Barbara Moo -- Jeff Martin

Now at InfoQ:

Learning Modern C++: An Interview with Barbara Moo

by Jeff Martin

The popularity of C++ has varied throughout the years since its introduction in the 1980s.  The rise of managed languages like Java and C# along with the emergence of scripting languages like JavaScript, Python, and Ruby has affected C++'s adoption.  Yet many supporters like C++ for the control, raw power, and speed that it offers.  C++11 promises to bring that power to programmers in a more efficient manner, and the changes it introduces illustrate how much the language has grown in the past 30 years.  Programmers looking to learn about C++11 or perhaps sample C++ for the first time would do well to try C++ Primer, 5th Edition by Stanley B. Lippman, Josée Lajoie, and Barbara E. Moo.  InfoQ had the opportunity to speak with Ms. Moo about her new book and the C++ language as a whole.

Continue reading...

What's new in C++11? -- KDAB

KDAB is now offering three-day training courses in C++11.

What’s New in C++11?

This three-day training teaches everything about the new C++ standard, C++11.

Course description

Table of contents (PDF)

C++11 will become more and more important in the C++ ecosystem, eventually becoming the most prevalent version. Every professional developer should invest in learning the new language version and try introducing its benefits into projects. And for good reasons: C++11 brings a large range of new features that makes development safer, faster, easier and more fun. Once you have tried features like lambda functions, range-based for loops, the auto keyword and the new initialization syntax, you won't want to go back. In addition to that, many more advanced features like variadic templates, rvalue reference and of course the new standard library additions like multithreading classes, smart pointers, regular expressions and new containers and algorithms complete the picture.

During the training day at Qt DevDays 2012 in Berlin, KDAB engineer Marc Mutz, presented some the most important C++11 features using parts of the material from this course. This was very well attended, receiving positive feedback.

Our full training lasts for three days and covers a wide range of topics, it goes in-depth and provides time to show C++11 examples as well as allowing participants to go hands-on and trying out C++11 themselves in exercise projects.

See the course description for more details about the content.

To view our schedule and to book your place for our next C++11 trainings go to: www.kdab.com/schedule/

C++ and Beyond 2013 dates and location finalized

The final dates and location are now set for C++ and Beyond 2013 with Scott Meyers, Herb Sutter, and Andrei Alexandrescu:

December 9-12, 2013 in beautiful Snoqualmie, Washington, USA.

From Scott Meyers' announcement:

About a month ago, I posted tentative dates for C&B 2013.  I cautioned that there was no contract yet, and I’m glad I did, because shortly thereafter we discovered an off-by-one scheduling snafu.  As a result, the dates are not the ones I posted earlier, they’re a day later: Monday evening, December 9, through Thursday, December 12.

The inital C&B in 2010 was held at the Salish Lodge and Spa in Snoqualmie, Washington, USA.  In 2011, we had a larger group in a larger venue, and last year we bumped up the numbers again.   Growth was ours, it seemed, but we sensed that C&B was looking more like a conventional conference and less like the unique event we had originally envisioned. For 2013, we decided to return to our roots, both geographically and organizationally.

C&B 2013 will return to the Salish Lodge and Spa in Snoqualmie, Washington (not far from Seattle). Enrollment will again be limited to the capacity of the ballroom (~64 attendees). Scott will again lead lunchtime walks. Evenings will again feature free-form “hang out with the speakers” sessions. Hotel guestrooms will again boast fireplaces, whirlpool tubs for two, and one whopping big waterfall just steps from the front door. If you were part of C&B 2010, you know what I’m talking about. If you weren’t, ask around: you’ll wish you had been.

We’ll announce more details when they’ve been finalized, including when registration for C&B 2013 will begin. In the meantime, reserve December 9-12 for C++ and Beyond 2013 in Snoqualmie, Washington, USA.

Stroustrup’s Tour of C++: Fourth chapter posted

The final installment of Bjarne Stroustrup's four-part Tour of C++ is now available. This material is a preview draft of Chapter 5 of Stroustrup’s upcoming The C++ Programming Language, 4th Edition.

A Tour of C++, Part 4: Concurrency and Utilities

by Bjarne Stroustrup

Bjarne writes:

Describe all of C++ in 100 pages (or less). Don't just describe the language, include the standard library. Don't use "white lies" to simplify. Describe the major programming styles and techniques. Give rationale. Don't forget about concurrency. And, oh, by the way, make it readable to programmers (do not require a PhD).

That was the task I set myself when I decided to write the "Tour of C++" for TC++PL4. I suspect that succeeding perfectly is beyond me, but at least I met the first criteria: The tour is currently 98 pages, and shrinking.

This last, part 4, of the tour presents concurrency and some of the newer standard-library facilities.

Enjoy!

See the whole Tour here.

Closer to Perfection: Get to Know C++11 Scoped and Based Enum Types -- Danny Kalev

Here's a nice intro and overview of one of the smaller features that makes C++11 safer.

Speaking of scoped and based enums, here's an interesting historical tidbit you may not know: These were initially co-proposed for C++ by an expert working on mission- and life-critical software and a large horizontal software company. Just goes to show the broad applicability of features like these, that matter where safety is critical and also help everyone.

Closer to Perfection: Get to Know C++11 Scoped and Based Enum Types

by Danny Kalev

C++ enum types pack a set of related constants in an intuitive and efficient user-defined type. Can you ask for more? With two new C++11 enhancements, namely scoped enums and based enums, the answer is "yes." Find out all about the recent facelift that C++11 enums underwent and learn how to refactor your code to benefit from the new enum features – without sacrificing performance or backward compatibility.

From the intro:

Enums are one of my favorite C++ features. They exemplify the notion of an efficient user-defined type without the heavy machinery of virtual functions, constructors, etc. (Compare C++ enums to other programming languages that still insist on using a full-blown class instead, and you’ll see what I mean.)

Yet, traditional enum types aren't flawless. ... C++11 addresses these issues with revamped enumerations that give you tighter control over the scope, size, and implicit conversions of enum types. Let's look at these new features more closely, and examine how they can improve both our code quality and frustration level.

Continue reading...

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