Articles & Books

How do you write a universal memoization function in C++? -- StackOverflow

From Python to C++:

Writing Universal memoization function in C++

Looking for a way to implement a universal generic memoization function which will take a function and return the memoized version of the same?

Looking for something like @memo (from Norving's site) decorator in python.

def memo(f):
    table = {}
    def fmemo(*args):
        if args not in table:
            table[args] = f(*args)
        return table[args]
    fmemo.memo = table
    return fmemo

Going more general, is there a way to express decorators in C++?

The Point of No Return -- bulldozer00

A cute nugget about [[noreturn]]:

The Point of No Return

by bulldozer00

As part of learning the new feature set in C++11, I stumbled upon the weird syntax for the new “attribute” feature: [[ ]]. One of these new C++11 attributes is [[noreturn]]. ...

Quick Q: When should you use constexpr? -- StackOverflow

Quick A: When you want to potentially evaluate a calculation at compile time. You can do much, much more than just "return 5;".

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there?

// preprocessor.
#define MEANING_OF_LIFE 42
// constants:
const int MeaningOfLife = 42;
// constexpr-function:
constexpr int MeaningOfLife () { return 42; }

It seems to me that if I wrote a function that return a literal value, and I came up to a code-review, someone would tell me, I should then, declare a constant value instead of writing return 5.

C++'s Best Feature -- Andrzej KrzemieĊ„ski

Andrzej's title is not only catchy, but completely correct (we politely disagree with your final disclaimer, sorry Andrzej): Deterministic lifetime with destructors is C++'s best feature, and very underappreciated. Like most wonderful things, you appreciate it most when it's gone, namely when you're using another language where it's absent.

C++'s best feature

by Andrzej Krzemieński

From the article:

C++, if you want to learn all of it, is big, difficult and tricky. If you look at what some people do with it, you might get scared. New features are being added. It takes years to learn every corner of the language.

But you do not need to learn all of it. Effective use of C++ requires only the knowledge of a couple of its essential features. In this post, I am going to write about one C++ feature that I consider one of the most important. The one that makes me choose C++ rather than other popular programming languages. ...

Sometimes You Must Violate an Abstraction to Maintain It -- Andrew Koenig

Koenig explains std::move as, well, just a bit of a fib, really:

Sometimes You Must Violate an Abstraction to Maintain It

by Andrew Koenig

From the article:

What std::move really does is to return its argument as an rvalue reference. In effect, every time we use std::move, we are telling a lie. In this case, by writing std::move(t.s), we are saying that we want to use t.s, but to do so in a way that treats t.s as an rvalue. It is acceptable for us to tell this lie for exactly the same reason that it is acceptable for us to cast t.s to string&& in the previous example: We know that t.s is a member of t, and t really refers to an rvalue in our caller's context.

We can tell such lies any time we are willing to take responsibility for the consequences. ...

Quick Q: Can I use a const std::regex from two threads without synchronization? -- StackOverflow

Quick A: See GotW #6a. The answer is the same for any object, not just regex.

Concurrently using std::regex, defined behaviour?

[...] I can't find anything which states whether using a const std::regex concurrently results in undefined behaviour or not. As far as I can tell, no edits are being made to the regex object so no undefined behaviour should be induced by using it concurrently?

Thanks in advance!

C++11/14 Training Materials -- Scott Meyers

meyers-newcppmaterials.PNGC++14 is still very much a draft -- it only became feature-complete in April and is now in its primary comment ballot. But interest in this new standard is high, with a restarted "Guru of the Week" series focusing on C++14 and now Scott Meyers has produced the first book-like materials that include significant coverage of C++14.

C++11 Training Materials Updated -- Now With C++14 Info!

For the seventh time since originally releasing them over three years ago, I've updated my annotated training materials for "The New C++". Until this update, "the new C++" referred to C++11, but with this revision, I'm including treatment of several features from draft C++14 that I believe will make it into the new new standard. As far as I know, this makes my training materials the first "book-like" publication that covers features in C++14.

What is std::promise? -- StackOverflow

Inquiring minds want to know this classic question:

What is std::promise?

I'm fairly familiar with the new standard library's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward.

However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The standard document itself doesn't contain a whole lot of information beyond its class synopsis, and neither does just::thread.

Could someone please give a brief, succinct example of a situation where an std::promise is needed and where it is the most idiomatic solution?

Quick Q: Why is noexcept not enforced at compile time? -- StackOverflow

NoSenseEtAl asked:

Why noexcept is not enforced at compile time?

As you might know C++11 has noexcept keyword. Now ugly part about it is this:

Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions.

http://en.cppreference.com/w/cpp/language/noexcept_spec

So is this a design failure on the committee part or they just left it as an exercise for the compile writers smile in a sense that decent compilers will enforce it, bad ones can still be compliant?