intermediate

Quick Q: Why are static addresses constexpr?

Quick A: Because it is defined at compile time in a special section.

Recently on SO:

A little confused about constexpr functions

If 'x' is declared static, there are no errors. Why?

This is because there is always exactly one x in the program. It has an address (somewhere in the .data segment under normal conditions).

Confusingly, both static and extern keywords specify the storage duration as static (they differ in linkage)

How is it possible to get a variable address during the compilation process? Aren't they allocated at run-time?
Variables with automatic, dynamic or thread storage durations are allocated at runtime. Static duration variables are allocated by the compiler. (The linker and OS can change the location, but they know how to fix all the references when they do)

 

The reviews at r/cpp_review have begun!

Participate in the first two reviews at r/cpp_review:

The reviews have begun

by Jens Weller

From the article

A few weeks ago I announced a C++ review community, which since then has grown to 250+ members on reddit. There has been great feedback and discussions since then, so that the idea is now ready to be tested.  With August, the first review period has started

(Not Really So) New Niche for C++: Browser!? -- No Bugs Hare

In this article "No Bugs" Hare outlines the possibility to run C++ code in the major four web browsers.

(Not Really So) New Niche for C++: Browser!?

by "No Bugs" Hare

From the article:

For quite a long while, C++ had been losing popularity; for example, as reported in [Widman16], in 2016 it got 7% less of the listings on Dice.com compared with a year earlier; and according to [TIOBE17], from the C++ Golden Age in 2004 till 2017, the C++ share fell from ~17% to a measly 6%.

As all of us (as in, ‘hardcore C++ fans’) know , this has nothing to do with the deficiencies of C++; rather it is related to an observation that the time of downloadable clients (which was one of the main C++ strongholds) has changed into the time of browser-based clients – and all the attempts to get C++ onto browsers were sooo ugly (ActiveX, anyone?) that this didn’t really leave a chance to use C++ there.

Well, it seems that this tendency is already in the process of being reverted:

C++ can already run on all four major browsers – and moreover, it has several all-important advantages over JavaScript, too.
And this – not too surprisingly – is what this article is all about.

C++17 attributes - maybe_unused, fallthrough and nodiscard--Simon Brand

Do you know these new attributes?

C++17 attributes - maybe_unused, fallthrough and nodiscard

by Simon Brand

From the article:

C++17 adds three new attributes for programmers to better express their intent to the compiler and readers of the code: maybe_unused, fallthrough, and nodiscard. This is a quick post to outline what they do and why they are useful.

Quick Q: Why is the count of weak_ptr tracked also?

Quick A: To be able to know when to delete the control block.

Recently on SO:

Why shared_ptr's reference counting object needs to keep track of the number of weak_ptrs pointing to the object too?

std::weak_ptr refers to the control block to know if the object still exists and if so, to provide a std::shared_ptr to it when needed. For that reason, the control block must exist as long as either a std::weak_ptr or a std::shared_ptr exists. You need to track the number of instances of std::weak_ptr to know when the last one is destroyed, just like for std::shared_ptr.

Quick Q: How to (and who can) implement the standard library features defined by the C++ committee?

Quick A: Everyone by reading and applying the C++ standard.

Recently on SO:

How to (and who can) implement the standard library features defined by the C++ committee?

The committee does not release any reference implementations. In the early days, things got standardized and then the tool developers went away and implemented the standard. This has changed, and now the committee looks for features that have been implemented and tested before standardization.

Also major developments usually don't go directly into the standard. First they become experimental features called a Technical Specification or TS. These TS may then be incorporated into the main standard at a later date.

You are free to write you own implementation of the C++ standard library. Plum Hall has a test suite (commercial, I have no connection, but Plum Hall are very involved with C++ standardization).

I don't see any issue with not being conformant. Almost all implementations have some extensions. Just don't make any false claims, especially if you want to sell your product.

If you're interested in getting involved, this can be done via your 'National Body' (ANSI for the USA, BSI for the UK etc.). The isocpp web site has a section on standardization which would be a good starting place.

Your own error code--Andrzej Krzemieński

The stl can help you!

Your own error code

by Andrzej Krzemieński

From the article:

I was recently implementing the “classification of error conditions” in my application offered by the functionality behind std::error_code. In this post I want to share some of my experience and insight.

Quick Q: How to make my custom type to work with “range-based for loops”?

Quick A: Create member functions begin() and end() returning an iterator.

Recently on SO:

How to make my custom type to work with “range-based for loops”?

The standard has been changed since the question (and most answers) were posted in the resolution of this defect report.

The way to make a for(:) loop work on your type X is now one of two ways:

  • Create member X::begin() and X::end() that return something that acts like an iterator
  • Create a free function begin(X&) and end(X&) that return something that acts like an iterator, in the same namespace as your type X.

And similar for const variations. This will work both on compilers that implement the defect report changes, and compilers that do not.

CppCon 2016: std::accumulate: Exploring an Algorithmic Empire--Ben Deane

Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:

std::accumulate: Exploring an Algorithmic Empire

by Ben Deane

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

What is the most powerful algorithm in the STL? In the world? There are many cases to be made. But this talk explores what I think is a pretty good candidate, which C++ calls std::accumulate(). Tucked away in <numeric>, perhaps relatively unregarded when compared with workhorses like std::find_if() and std::partition(); nevertheless, std::accumulate() is in some sense the ur-algorithm on sequences.

Let’s explore the result of looking at code through an accumulate-shaped lens, how tweaking the algorithm for better composability can unlock many more uses, and how it can be further genericized with applications to parallelism, tree structures, and heterogeneous sequences.

std::accumulate(): it’s not just for adding things up!