Articles & Books

The Curious Case of the Recurring Template Pattern -- K-Ballo

Do you remember CRTP? No? Then definitely read about it here:

The Curious Case of the Recurring Template Pattern

by K-ballo

Inheritance is a handy tool to simplify design and avoi code duplication. However, good old fashioned inheritance is not always the right tool for the job. What if a base class could know, at compile time, who derives from it? Enter the curiously recurring template pattern (CRTP)...

Quick Q: A lambda's return type can be deduced, so why can't a function's? -- StackOverflow

Quick A: It can, in C++14. In fact, this C++14 feature supported today in current versions of GCC and Clang with -std-c++1y and in the Visual C++ November 2013 CTP.

On SO:

A lambda's return type can be deduced by the return value, so why can't a function's?

#include <iostream>

int main(){

    auto lambda = [] {
        return 7;
    };

    std::cout << lambda() << '\n';

}

This program compiles and prints 7.
The return type of the lambda is deduced to the integer type based on the return value of 7.

Why isn't this possible with ordinary functions?

#include <iostream>

auto function(){
    return 42;
}

int main(){

    std::cout << function() << '\n';
}

error: ‘function’ function uses ‘auto’ type specifier without trailing return type

Quick Q: Why have move semantics? -- StackOverflow

Quick A: Because value semantics are clean and naturally (exception-)safe, and avoid the brittleness of pre-C++11 workarounds that resort to owning raw pointers.

As often happens, the question contains the answer...

Why have move semantics?

... Can't the client already take advantage of pointers to do everything move semantics gives us? If so, then what is the purpose of move semantics?

Move semantics:

std::string f()
{
    std::string s("some long string");
    return s;
}
int main()
{
    // super-fast pointer swap!
    std::string a = f();
    return 0;
}

Pointers:

std::string *f()
{
    std::string *s = new std::string("some long string");
    return s;
}

int main()
{
    // still super-fast pointer swap!
    std::string *a = f();
    delete a;
    return 0;
}

Acquire and Release Fences Don't Work the Way You'd Expect -- Jeff Preshing

The C++11 standard makes a distinction between acquire and release fences and acquire and release operations. The differences are important and can affect correctness as well as performance.

[Ed.: These correctness subtleties are another reason to avoid standalone fences... in addition to the notes in this article, there are performance reasons to do so, as mentioned in Sutter's linked talk. std::atomics are the correct tool in nearly all cases where in the past you'd have reached for a standalone fence.]

Acquire and Release Fences Don't Work the Way You'd Expect

by Jeff Preshing

From the article:

... It's perhaps surprising, then, that this definition does not apply to standalone acquire and release fences in C++11! Those are a whole other ball of wax.

To see what I mean, consider the following two code listings. They’re both taken from my post about the double-checked locking pattern in C++11. The code on the left performs a release operation directly on m_instance, while the code on the right uses a release fence instead. ...

CppQuiz.org officially launched -- Anders Schau Knatten

Anders Schau Knatten recently launched the new site CppQuiz.org inspired largely by Olve Maudal's C++ pub quizzes. So grab a refreshing beverage, pull up a chair, and try a few to start off the first week of December...

CppQuiz.org officially launched!

by Anders Schau Knatten

From the announcement:

What is it

CppQuiz.org is (as you might have guessed by now) an online C++ quiz. Each question is a full C++ program, and you are to figure out what its output is. I stole this format from Olve Maudal's pub quizzes, but with one major difference: While his quizzes are about what happens on his computer (which is very interesting for a more interactive format), CppQuiz.org asks about what the standard mandates the output to be. If the example code doesn’t compile, or has unspecified/undefined behaviour, you answer that.

The site will just keep throwing questions at you (training mode), optionally giving you a hint and finally give you a full explanation of the answer, with references to the C++11 standard. If you want, you can however start a new quiz (quiz mode), and get a fixed number of questions. At the end you get a score, and a link to give your friends to see if they can beat you. Neither mode requires you to register or log in.

How you can help

If you like the quiz and want to help, there are many ways to do so:

  • Create your own questions, as many have done
  • Help improve the design (pull requests, e-mail, Twitter)
  • Help improve the functionality (pull requests, e-mail, Twitter)
  • Any other feedback (comments to this post, e-mail, Twitter)

Tips for founding new (C++) user groups -- Jens Weller

We're seeing quite a few new C++ user groups being formed, and so it's a good time to link to this timely article by Jens Weller:

Founding local C++ User Groups

From the article:

Lets start into the discussion on founding user groups, there is in my opinion different approaches to get started, but I don't want this to be a discussion, so I'll just list what I think is right. First I think that a C++ User Group should be local, which means its usually for a certain region. From my expierence, people are willing to travel up to 70km one way to a user group meeting. So in order to get started, I think you'll need the following four points:

  • People
  • Location
  • Topics
  • Date

...

Quick Q: When do you need to declare a variable constexpr? -- StackOverflow

Quick A: When you want to use the variable in a way that requires its value to be known at compile time.

Here's a short nugget that helps demonstrate the meaning of constexpr:

Why is constexpr required even though member function is constexpr?

The following does not compile unless I put constexpr before initializer_list:

constexpr std::initializer_list<int> il = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
std::array<int, il.size()> a;

But initializer_list size is constexpr:

constexpr size_type size() const;

What are inline namespaces good for? -- StackOverflow

Quick A: For source-level library versioning.

A StackOverflow classic:

What are inline namespaces for?

C++11 allows inline namespaces, all members of which are also automatically in the enclosing namespace. I cannot think of any useful application of this -- can somebody please give a brief, succinct example of a situation where an inline namespace is needed and where it is the most idiomatic solution?

(Also, it is not clear to me what happens when a namespace is declared inline in one but not all declarations, which may live in different files. Isn't this begging for trouble?)

Quick Q: Why write "5 == myValue" instead of "myvalue == 5"? -- StackOverflow

Quick A: Because it catches most cases where you accidentally wrote = instead of ==.

From SO:

Reason for using '5 == myValue' in conditionals

I've come across some code that flips how a condition is checked and was wondering why this would be done aside from a weird personal quirk. I've never seen any text books use it nor have I seen any sample code done this way.

// why do it this way?
if (5 == myValue)
{
    // do something
}

// instead of:
if (myValue == 5)
{
    // do something
}

I've only seen this way for == operand but not for any other operands.

Deleted Functions in C++11 -- Fang Lu

Here is a quick overview of =delete from the IBM Cafe's tour of C++ features.

Note: This summary article focuses on applying =delete to special member functions, which is a main use case. However, be aware that that's only part of the story, because =delete can also apply to regular member function and free function overloads to suppress specific overloads with nice compile-time errors. We invite authors to write about this aspect as well -- just write about it on your own blog and and send us a link to your post via 'Suggest an Article' at the top of this page (you must be logged in to isocpp.org to see this option).

Deleted functions in C++11

by Fang Lu

The deleted functions feature is introduced into the C++11 standard. In this article, I will explain this feature and provide some examples on how to use it...