Articles & Books

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

Concept Checking in C++11 -- Eric Niebler

While we're waiting for Concepts Lite, Eric shows how we can already do quite a bit in C++11 while planning for a transition to language support when it's available.

Concept Checking in C++11

by Eric Niebler

From the article:

This post describes some utilities I’ve recently developed for doing concept checking in C++11. These utilities are part of an ongoing project to reimplement ranges, also for C++11, but I think the concept checking utilities are useful and interesting in their own right...

Basic Structure of C++ Program -- Prashant Sharma

[Ed.: This is a simple short "hello world" overview for someone starting to program in C++, and is basically correct. There are lots of expert-friendly articles, but we like and want to encourage the beginner- and intermediate-level articles as well for the benefit of the many recent newcomers to the language.]

Basic Structure of C++ Program

The easiest way to understand the basic structure of C++ program is by writing a program. The basic C++ program is as follows: ...

Quick Q: Why is std::vector contiguous? -- StackOverflow

Quick A: Because if you're serious about performance, you'll often (not always) use contiguous arrays.

From SO:

Why is std::vector contiguous?

Besides the fact that the standard defines it to be contiguous, why is std::vector contiguous?

If it runs out of space, it needs to reallocate a new block and copy the old block to the new one before continuing.

What if it wasn't contiguous? When the storage fills up, it would just allocate a new block and keep the old block. When accessing through an iterator, it would do simple >, < checks to see which block the index is in and return it. This way it doesnt need to copy the array every time it runs out of space.

Would this really work/be better? or am i missing something?

C++11 Annoyance Avoiders -- Tony DaSilva

A useful short nugget: override FTW!

C++11 Annoyance Avoiders

by Tony DaSilva

From the article:

I’ve always been torn as to whether or not to annotate derived class member functions intended to override base class members as virtual...

Type Erasure, Part 1 -- Andrzej KrzemieĊ„ski

What is this "type erasure" thing you speak of? It's not something at the other end of a pencil (remember those?) but a way to hold an object without knowing its exact type. Andrzej explains:

Type Erasure, Part 1

by Andrzej Krzemieński

From the article:

Have you ever came across term type erasure in C++? This “pattern” or “technique” is growing more and more popular. In this post I will try to describe what it is. Note that it is something different than a similar term in Java.

What does type encode?

Let’s start with describing the opposite of type erasure. We could call such situation “type-full-ness”. Forget the term though, let me illustrate what I mean with an example. ...

Quick Q: Why is there a nonmember begin(), but no nonmember cbegin()? -- StackOverflow

Quick A: There is, in C++14.

From SO:

Is there a way to use standalone std::begin and for a const_iterator?

I like consistency. I recently asked the question of using std::begin vs. e.g. std::vector<int>::begin, and the unanimous decision seemed to be to use the former since it is more general. But I think I found a stick in the mud. Sometimes, you want to convey you will not change a container as you loop through it, hence calling std::vector<int>::cbegin. It would make your code quite asymmetric if you sometimes did iter = v.cbegin() and other times did iter = begin(v). Is there a way around this lack of symmetry, and would you still recommend std::begin given this knowledge? Why does C++ not have std::cbegin?

Quick Q: Can you move out of an initializer_list? -- StackOverflow

Quick A: No, because the contents are const.

A classic from SO, recommended by a developer team that encountered this situation again in the past week:

initializer_list and move semantics

Am I allowed to move elements out of a std::initializer_list<T>?

#include <initializer_list>
#include <utility>

template<typename T>

void foo(std::initializer_list<T> list)
{
    for (auto it = list.begin(); it != list.end(); ++it)
    {
        bar(std::move(*it));   // kosher?
    }
}

Since std::intializer_list<T> requires special compiler attention and does not have value semantics like normal containers of the C++ standard library, I'd rather be safe than sorry and ask.