intermediate

You Don't Know const and mutable -- Herb Sutter

In August, Herb Sutter gave a brand new 30-min talk at C++ and Beyond where he laid out the fundamentally new meanings of two long-time C++ keywords:

You Don't Know [const] and [mutable]

Herb Sutter

There’s a major change in C++11 that [...] rewrites pre-C++11 design guidance and is directly related to writing solid code in a concurrent and parallel world. And it isn’t just academic — everyone is going to have to learn and apply the new C++11 guidance that we’ll cover in this session.

This is a great example of how C++11 is a simpler language: We can stop the Cold War-era waffling about subtleties about what 20th-century C++ const means, and proudly declare modern C++ const has the simple and natural and "obvious" meaning that most people expected all along anyway.

One of the most common questions after the talk was, "Do other C++ experts agree with Herb's conclusions?" The answer is yes -- regarding his upcoming The C++ Programming Language, Fourth Edition, Bjarne Stroustrup writes: "I do point out that const means immutable and absence of race conditions in the last Tour chapter. I plan more for the concurrency chapter." Look for the third Tour chapter to be posted here next week, and the aforementioned last Tour chapter to be posted here in early February.

Enjoy.

Using constexpr to Improve Security, Performance and Encapsulation in C++ -- Danny Kalev

How is constexpr different from const, and how does it contribute to making modern C++ code cleaner and simpler, as well as faster than ever? Danny Kalev gives a nice summary:

Using constexpr to Improve Security, Performance and Encapsulation in C++

by Danny Kalev

constexpr is a new C++11 keyword that rids you of the need to create macros and hardcoded literals. It also guarantees, under certain conditions, that objects undergo static initialization. Danny Kalev shows how to embed constexpr in C++ applications to define constant expressions that might not be so constant otherwise.

Continue reading...

Quick Q: Inserting a variadic argument list into a vector? -- StackOverflow

From StackOverflow [c++11]:

I have an object that needs to take a variadic argument list in its constructor and store the arguments in a vector. How do I initialize a vector from a the arguments of a variadic constructor?

class GenericNode {
public:
    GenericNode(GenericNode*... inputs) {
            /* Something like... */
        // inputs_.push_back(inputs)...;
}
private:
    std::vector<GenericNode*> inputs_;
};

Continue reading...

Three Optimization Tips for C++ -- Andrei Alexandrescu

For your Friday viewing pleasure, from the always-engaging master, Andrei Alexandrescu:

Three Optimization Tips for C++ (video) (PDF slides)

Andrei Alexandrescu

Facebook NYC, December 4, 2012

 

From an early slide in the talk: "Intuition:

  • Ignores aspects of a complex reality
  • Makes narrow/wrong/obsolete assumptions

[...]

  • The only good intuition: "I should time this."

​Enjoy.

(Bonus: The tips are also applicable in other languages. They just happen to be easier to use and control in C++.)

Systematic Error Handling in C++ -- Andrei Alexandrescu

Channel 9 has just posted a video of Andrei Alexandrescu’s "Systematic Error Handling in C++11" presented at C++ and Beyond 2012 last summer in Asheville, NC. This is a great 90-minute talk with useful insights and techniques for programming in general and programming in C++11 in particular.

Systematic Error Handling in C++11

Andrei Alexandrescu

Writing code that is resilient upon errors (API failures, exceptions, invalid memory access, and more) has always been a pain point in all languages. This being still largely an unsolved (and actually rather loosely-defined) problem, C++11 makes no claim of having solved it. However, C++11 is a more expressive language, and as always more expressive features can be put to good use toward devising better error-safe idioms and libraries.

This talk is a thorough visit through error resilience and how to achieve it in C++11. After a working definition, we go through a number of approaches and techniques, starting from the simplest and going all the way to file systems, storage with different performance and error profiles (think HDD vs. RAID vs. Flash vs. NAS), and more. As always, scaling up from in-process to inter-process to cross-machine to cross-datacenter entails different notions of correctness and resilience and different ways of achieving such.

To quote a classic, "one more thing"! An old acquaintance -- ScopeGuard -- will be present, with the note that ScopeGuard11 is much better (and much faster) than its former self.

Exception-Safe Coding in C++ -- Jon Kalb

Jon Kalb from the C++ Now 2012 conference.

Exception-Safe Coding in C++

by Jon Kalb

Are you 100% confident that your code is exception-safe?

Safe usage of exceptions is a non-trivial problem that the industry has struggled with for the better part of two decades. If you have fear, uncertainty, or doubt about exception safety or just want to see the best practices for using exceptions in C++, this session is for you. We'll start with "What is the problem we are trying to solve?" and discuss alternatives, acknowledge the challenges associated with with exception usage, and cover some well-meaning but misguided attempts at safety. I will then present a set of guidelines that are the basis for safe exception usage and solid implementation techniques, including how to transition from an exception-unsafe legacy code base.

When we are finished you will know how to produce code that is easier to write, easier to understand, faster, and 100% robust in the face of exceptions.

This is a two part video series. Links:

Enjoy!

C++ Benchmark: std::vector vs. std::list -- Baptiste Wicht

[Ed.: Another reason why vector is the default (not only, but preferred) standard container.]

C++ Benchmark: std::vector vs. std::list

by Baptiste Wicht

In C++, two of the most used data structures are std::vector and std::list. In this article, their performance is compared in practice on several different workloads.

It is generally said that a list should be used when random insert and remove will be performed (performed in O(1) versus O(n) for a vector). If we look only at the complexity, search in both data structures should be roughly equivalent, complexity being in O(n). When random insert/replace operations are performed on a vector, all the subsequent data needs to be moved and so each element will be copied. That is why the size of the data type is an important factor when comparing those two data structures.

However, in practice, there is a huge difference, the usage of the memory caches. All the data in a vector is contiguous where the std::list allocates separately memory for each element. How does that change the results in practice ?

This article shows the difference in performance between these two data structures. 

Continue reading...

Rule of Zero -- R. Martinho Fernandes

Long-time C++98 developers know about the Rule of Three. But with C++11's addition of move semantics, we need to be talking instead about the Rule of Five.

This article argues for limiting the use of the Rule of Five, and instead mostly aiming for a "Rule of Zero" -- why you don't have and don't want to write logic for copying, for moving, and for destruction all the time in C++, and why you should encapsulate that logic as much as possible in the remaining situations when you actually need to write it. (And, in passing, he shows once again why C++11 developers seem to be drawn to reusing unique_ptrs with custom deleters like moths to flames bees to flowers. See also recent examples like this one on std-proposals.)

Rule of Zero

In C++ the destructors of objects with automatic storage duration are invoked whenever their scope ends. This property is often used to handle cleanup of resources automatically in a pattern known by the meaningless name RAII.

An essential part of RAII is the concept of resource ownership: the object responsible for cleaning up a resource in its destructor owns that resource. Proper ownership policies are the secret to avoid resource leaks...

Continue reading...