Articles & Books

Compiling and Linking in C++ -- Rusty Ocean

Selection_101.pngLife'n'gadget just published a nice overview of the basic C++ compilation model, useful for people who are new to programming in C++.

Compiling and Linking in C++

by Rusty Ocean

From the article:

When you write a C++ program, the next step is to compile the program before running it. The compilation is the process which convert the program written in human readable language like C, C++ etc into a machine code, directly understood by the Central Processing Unit. There are many stages involved in creating a executable file from the source file. The stages include Preprocessing, Compiling and Linking in C++...

Why does std::condition_variable take a std::unique_lock instead of a std::mutex? -- StackOverflow

Recently on StackOverflow, someone asked: What's the job of std::unique_lock when used with std::condition_variable::wait()?

A more detailed answer is available in this earlier question, including why taking a unique_lock makes the C++ version superior to some other libraries' designs:

C++11: Why does std::condition_variable use std::unique_lock?

I am a bit confused about the role of std::unique_lock when working with std::condition_variable. As far as I understood the documentation, std::unique_lock is basically a bloated lock guard, with the possibility to swap the state between two locks.

I've so far used pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) for this purpose (I guess that's what the STL uses on posix). It takes a mutex, not a lock.

What's the difference here? Is the fact that std::condition_variable deals with std::unique_lock an optimization? If so, how exactly is it faster?

 

Trip report: Fall ISO C++ meeting -- Michael Wong

Michael Wong, head of Canadian delegation, has posted Part 1 of his report on the September ISO C++ meeting held in Chicago.

The View from the C++ Standard meeting September 2013 Part 1

by Michael Wong

From the article:

At this meeting, the most important thing was to address as many of the National Body (NB) Comments from the draft C++14 CD possible. This will enable us to be in good shape for the release of C++14 in 2014. Please look at my blog series to get an idea of the major content. However, this meeting did have some interesting minor changes which modified that content. This is fairly normal to decouple features which is still controversial. The biggest change is the moving of VLA (or what we called Array of Runtime Bound) and dynarray into a library array TS, and the adoption of the single quote as a digit separator for C++14.

C++11 and Boost

What issues arise when combining C++11 and (older) Boost code that has pre-standard versions of C++11 features?

C++11 and Boost

by Jens Weller

From the article:

Some parts of the Standard Library in C++11 are predated in boost. When playing around with C++11, you get used to using some parts in the Standard Library that are used in C++03 with their boost counterpart. Also, there is some libraries now occuring, which are C++11 based, so interfacing with either boost or C++11 code is soon an issue.

No Runtime Overhead -- Bulldozer00

sharedunique.pngA little nugget about the free-as-in-no-overhead-ness of unique_ptr and std::move:

No Runtime Overhead

by Bulldozer00

From the article:

Unless I really need shared ownership of a dynamically allocated object, which I haven’t so far, I stick to the slimmer and more performant std::unique_ptr. ...

Out Parameters, Move Semantics, and Stateful Algorithms -- Eric Niebler

In this article, Eric Niebler discusses an issue of API design regarding the age-old question of out parameters versus return-by-value, this time in light of move semantics. He uses std::getline as his example.

Out Parameters, Move Semantics, and Stateful Algorithms

by Eric Niebler

From the article:

I think getline is a curious example because what looks at first blush like a pure out parameter is, in fact, an in/out parameter; on the way in, getline uses the passed-in buffer’s capacity to make it more efficient. This puts getline into a large class of algorithms that work better when they have a chance to cache or precompute something.

 

Double-Checked Locking Is Fixed in C++11 -- Jeff Preshing

preshing.PNGJeff Preshing gives a nice overview of the on-again/off-again/on-again status of a common approach to lazy initialization.

[Ed: Note that DCLP is not just for thread-safe singletons, that's just a handy example. It's for lazy initialization in general.]

Double-Checked Locking Is Fixed in C++11

by Jeff Preshing

From the article:

The double-checked locking pattern (DCLP) is a bit of a notorious case study in lock-free programming. Up until 2004, there was no safe way to implement it in Java. Before C++11, there was no safe way to implement it in portable C++.

As the pattern gained attention for the shortcomings it exposed in those languages, people began to write about it. In 2000, a group of high-profile Java developers got together and signed a declaration entitled “Double-Checked Locking Is Broken”. In 2004, Scott Meyers and Andrei Alexandrescu published an article entitled “C++ and the Perils of Double-Checked Locking”. Both papers are great primers on what DCLP is, and why, at the time, those languages were inadequate for implementing it.

All of that’s in the past. Java now has a revised memory model, with new semantics for the volatile keyword, which makes it possible to implement DCLP safely. Likewise, C++11 has a shiny new memory model and atomic library which enable a wide variety of portable DCLP implementations. C++11, in turn, inspired Mintomic, a small library I released earlier this year which makes it possible to implement DCLP on some older C/C++ compilers as well.

In this post, I’ll focus on the C++ implementations of DCLP...

What are the semantics of =delete? -- StackOverflow

The question, rephrased: Does =delete mean "make the compiler skip this function and keep looking," or "make this function unusable and make the caller fix their code" -- and why?

What's the exact semantics of a deleted function in C++11?

struct A
{
    A();

    A(const A&);
    A& operator =(const A&);

    A(A&&) = delete;
    A& operator =(A&&) = delete;
};

struct B
{
    B();

    B(const B&);
    B& operator =(const B&);   
};

int main()
{
    A a;
    a = A(); // error C2280

    B b;
    b = B(); // OK
}

My compiler is VC++ 2013 RC.

error C2280: 'A &A::operator =(A &&)' : attempting to reference a deleted function

I just wonder why the compiler doesn't try A& operator =(const A&); when A& operator =(A&&) is deleted?

Is this behavior defined by the C++ standard?