Articles & Books

Quick Q: C++ constexpr - Value can be evaluated at compile time?

Quick A: if parameter are not known at compile time, it is like a normal function

Recently on SO:

C++ constexpr - Value can be evaluated at compile time?

The quoted wording is a little misleading in a sense. If you just take PlusOne in isolation, and observe its logic, and assume that the inputs are known at compile-time, then the calculations therein can also be performed at compile-time. Slapping the constexpr keyword on it ensures that we maintain this lovely state and everything's fine.

But if the input isn't known at compile-time then it's still just a normal function and will be called at runtime.

So the constexpr is a property of the function ("possible to evaluate at compile time" for some input, not for all input) not of your function/input combination in this specific case (so not for this particular input either).

It's a bit like how a function could take a const int& but that doesn't mean the original object had to be const. Here, similarly, constexpr adds constraints onto the function, without adding constraints onto the function's input.

Admittedly it's all a giant, confusing, nebulous mess (C++! Yay!). Just remember, your code describes the meaning of a program! It's not a direct recipe for machine instructions at different phases of compilation.

(To really enforce this you'd have the integer be a template argument.)

C++ Links #1--Bartlomiej Filipek

Things to look at.

C++ Links #1

by Bartlomiej Filipek

From the article:

I'd like to make an experiment on the blog and introduce a new simple series. Each Friday you'll see a summary with valuable links and resources from the C++ World. The links and annotations are coming from a guest author - Wojciech Razik - one of the co-author of cpp-polska.pl.

Stack Factory Puzzle -- Alex Marmer

When instances of classes derived from the same class need to be created based on certain conditions, they are created on the heap. The puzzle is about how to do it on the stack.

Stack Factory Puzzle

By Alex Marmer

 

Mathematics behind Comparison #4: Three-Way Comparison--Jonathan Müller

Everything you need to know!

Mathematics behind Comparison #4: Three-Way Comparison

by Jonathan Müller

From the article:

In order to sort a collection of elements you need to provide a sorting predicate that determines when one element is less than the other. This predicate must “induce a strict total ordering on the equivalence classes” according to cppreference. Wait, what?

The upcoming C++ spaceship operator implements a three-way comparison, i.e. it is a single function that can return the results of <, == and > combined. But related to it are terms like “strong equality” and “weak ordering” which are somewhat confusing if you don’t have the mathematical background.

So let’s untangle it: This series will explain both the mathematics behind equality and ordering, as well as give concrete guidelines for implementing the comparison operators and the spaceship operator.

Now that we’ve covered both equivalence and ordering relations we can finally talk about the spaceship operator and three-way comparisons...

Quick Q: How to track newer C++ std documents of given topic?

Quick A: Use wg21.link

Recently on SO:

How to track newer C++ std documents of given topic?

For the newer proposals (ones that start with the letter P) you can use wg21.link redirect service to obtain the latest document:

wg21.link - WG21 redirect service.

Usage:
wg21.link/nXXXX
wg21.link/pXXXX
wg21.link/pXXXXrX Get paper.

wg21.link/standard
Get working draft.

wg21.link/cwgXXX
wg21.link/ewgXXX
wg21.link/lwgXXX
wg21.link/lewgXXX
wg21.link/fsXXX
wg21.link/editXXX
Get issue.

wg21.link/index.json
wg21.link/index.ndjson
wg21.link/index.txt
wg21.link/specref.json
Get everything.

wg21.link/
Get usage.

For example for P0476: Bit-casting object representations if we use wg21.link/P0476 we obtain the latest version which is P0476R2.

In my answer to How does the standards committee indicate the status of a paper under consideration? I go into more details of the WG21 site and what documents you can find there.

Use the everything link for Pre P proposals
If we use the wg21 redirect service Get Everything link we can do a text search for the paper title. So for your example Improvements to std::future<T> and Related APIs we can see the last document is N3857:

"N3857": {
"type": "paper",
"title": "Improvements to std::future and Related APIs",
"subgroup": "Concurrency",
"author": "N. Gustafsson, A. Laksberg, H. Sutter, S. Mithani",
"long_link": "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3857.pdf",
"link": "https://wg21.link/n3857",
"source": "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/",
"date": "2014-01-16"
},

Modern C++: 7 Ways to Fake It Until You Have It--Jonathan Boccara

If you want.

Modern C++: 7 Ways to Fake It Until You Have It

by Jonathan Boccara

From the article:

Do you wish you had a later version of C++ in your production code? If you do, you’re not alone: a lot of C++ developers today don’t work with a compiler that supports the latest version of the standard.

It could be for many reasons: perhaps you have a lot of legacy code to migrate, or your clients do, or your hardware doesn’t have the adequate infrastructure yet. The point is, you can’t benefit from the latest features that the language offers, and that’s a shame because some of them would surely make your code more expressive.

But even if you can’t use those features, you don’t have to give up on their benefits. At least some of their benefits. There are way you could use the ideas of the new features in your code, to convey your intents more precisely.

Sure enough, it’s not as good as having them natively, which is why updating your compilers is still a necessity. But in the meantime, here are 7 ways to emulate those features, that will improve your code at a minimal cost...

Data-Oriented Design and Avoiding the C++ Object-Oriented Programming Zimmer Frame -- Leigh Johnston

An article about the increasing importance of data-oriented design within the context of C++.

Data-Oriented Design and Avoiding the C++ Object-Oriented Programming Zimmer Frame

by Leigh Johnston

From the article:

Object-oriented programming, OOP, or more importantly object-oriented design (OOD) has been the workhorse of software engineering for the past three decades but times are changing: data-oriented design is the new old kid on the block.

Preprocessing Phase for C++17's Searchers--Bartlomiej Filipek

Searchers objects.

Preprocessing Phase for C++17's Searchers

by Bartlomiej Filipek

From the article:

Searchers from C++17 are a new way to perform efficient pattern lookups. The new standard offers three searchers: default_searcher , boyer_moore_searcher and boyer_moore_horspool_searcher. The last two implements algorithms that require some additional preprocessing for the input pattern. Is there a chance to separate preprocessing time from the search time?