intermediate

Functional Programming Is Not a Silver Bullet--Jonathan Boccara

Nothing is perfect.

Functional Programming Is Not a Silver Bullet

by Jonathan Boccara

From the article:

The past few years have seen a boost in popularity of the functional programming paradigm. Languages that were used mostly in academic circles for decades are now in broader use amongst programmers. And every couple of months, another functional language hits the news and gets its trail of followers.

Why is that? Functional programming allow for safer and more robust code, in part due to one of its core principles: values are not mutable. A consequence of this is that there is no side effects. We can apply this principle in any language, including in C++, by coding with the least side effects possible.

While it certainly helps putting together a better design of code, it’s important to realize that it’s not the panacea, that this principle doesn’t solve in itself all design issues. Nothing is the panacea anyway, but in this time of gold rush towards functional programming, we could be tricked into thinking it will automatically lead to good design.

Functional programming is known to reduce coupling in code. We’ll briefly go over what coupling is, what sort of coupling functional programming prevents, and how some other dangerous forms of coupling can still sneak in even with functional programming. You want to pay attention to those to preserve the design of your code...

What is an Allocator?--Vanand Gasparyan

Basic, but complex.

What is an Allocator?

by Vanand Gasparyan

From the article:

This is one of the many beautiful questions you can think on or be asked at interviews if your native coding language is C++. The beautiful thing about this question is that you answer it again and again as you grow and gain more experience. Similarly, when you’re being interviewed for an internship, knowing that there is such a thing somewhere in STL might be enough, whereas later you’ll be expected to write your own custom allocator...

A Free Ebook on C++ Smart Pointers--Jonathan Boccara

Nice way to learn more.

A Free Ebook on C++ Smart Pointers

by Jonathan Boccara

From the article:

To write expressive code in C++, mastering smart pointers is a necessity! Without them, our code becomes littered with memory management, news and delete, and unclear semantics about who owns what resources.

If you’re part of my mailing list (which you can join at the bottom of this post), you will get as a Christmas gift a free ebook of more than 50 pages of selected contents of Fluent C++ about smart pointers. Of course, you also get access to the ebook if you’re a Patron of Fluent C++...

How to Design Function Parameters That Make Interfaces Easier to Use (3/3)--Jonathan Boccara

The end.

How to Design Function Parameters That Make Interfaces Easier to Use (3/3)

by Jonathan Boccara

From the article:

This is the final article in the series about function parameters. This series contains:

  • Part 1: interface-level parameters, one-parameter functions, const parameters,
  • Part 2: calling contexts, strong types, parameters order,
  • Part 3: packing parameters, processes, levels of abstraction.

Quick Q: Type inference of 'auto' when using range-for loop in multidimensional arrays

Quick A: a pointer has no size information.

Recently on SO:

Type inference of 'auto' when using range-for loop in multidimensional arrays

The type of row is deduced to be int*. That means, just like any other int*, the compiler doesn't know how big the array it points to is, or even that it's a pointer to the first element of an array at all. All of that information is lost when an array decays into a pointer to its first element.

If instead you use something like

for (auto& row : ia) //<-- NOTE: row is now a reference
    for (int* j = std::begin(row); j != std::end(row); ++j)
        std::cout << *j << '\n';

then the type of row will be deduced to int (&)[4]: reference to an array of 4 ints. The length information is retained, so std::begin and std::end have the information they need.

PS: Just as a note: range-for works by using std::begin and std::end internally, so the above can be a bit more concisely written as

for (auto& row : ia)
    for (auto j : row)
        std::cout << j << '\n';