Articles & Books

C++ Links #5 -- Bartlomiej Filipek and Wojciech Razik

cpplinks.pngThe 5th episode of the 'most useful C++ links' is now available:

C++ Links #5

by Bartlomiej Filipek and Wojciech Razik

From the article:

Welcome to new C++ Links - most important and useful articles, podcasts and videos that happen between 29th September and 5th of October.

In this week you will find two trip reports from CppCon, an article about std::any (aka modern void*), a video about std::fmt library and many more!

C++ Links - 22nd - 28th September

4th episode of the most useful C++ links smile

C++ Links #4

by Bartlomiej Filipek & Wojciech Razik

From the article:

Welcome to new C++ Links - most important and useful articles, podcasts and videos that happen between 22th and 28th of September.

This week CppCon took place - the biggest C++ conference. In today’s list, you will find the first video from there, about future of C++, you can also see a post about removing duplicate elements from associative containers and an example of really defensive programming.

The case for Auto Non-Static Data Member Initializers -- Corentin Jabot

The case for Auto Non-Static Data Member Initializers

By Corentin Jabot

 

From the article:

In this article, we talk about Auto Non-Static Data Member Initializers in C++. All code snippet can be tested on Compiler Explorer thanks to Matt Godbolt.

In fact, the main motivation for this article is to put this feature in the hand of people to prove that it works and that it would be a great addition to the standard.

How to Remove Elements from a Sequence Container in C++--Jonathan Boccara

Using the std to remove.

How to Remove Elements from a Sequence Container in C++

by Jonathan Boccara

From the article:

As part of the STL Learning Resource, we’re tackling today the STL algorithms that remove elements from a collection.

Removing an element from a C++ collection can’t be that complicated, can it?

Well, how can I put it… It has a rich complexity, let’s say.

Ok, maybe it’s a little complicated.

We will cover this topic in a series of four articles:

  • How to Remove Elements from a Sequence Container (vector, string, deque, list)
  • How to Remove Pointers from a Vector in C++ (co-written with Gaurav Sehgal)
  • How to Remove Elements from an Associative Container (maps and sets)
  • How to Remove Duplicates from an Associative Container

C++ Links #2--Bartlomiej Filipek

New links to check out!

C++ Links #2

by Bartlomiej Filipek

From the article:

Welcome to new C++ Links - most important and useful articles, podcasts and videos that happened between 8th and 14th of September. Today you will find a link to a post about the C++ quality of life features, a video with an explanation of the difference between const and constexpr, an article that describes some of SFINAE problems and many others.

Modern C++ Features – Quality-of-Life Features--Arne Mertz

Some to help, some to enable.

Modern C++ Features – Quality-of-Life Features

by Arne Mertz

From the article:

With the new C++ standards, we got a lot of features that feel like “quality-of-life” features. They make things easier for the programmer but do not add functionality that wasn’t already there. Except, some of those features do add functionality we couldn’t implement manually.

Some of those quality-of-life features are really exactly that. The standard often describes them as being equivalent to some alternative code we can actually type. Others are mostly quality-of-life, but there are edge cases where we can not get the effect by hand, or the feature is slightly superior to the manual implementation.

I will concentrate on core language features here, since most library features are implementable using regular C++. Only a few library features use compiler intrinsics...

Quick Q: C++11 auto: what if it gets a constant reference?

Quick A: auto never deduces a reference.

Recently on SO:

C++11 auto: what if it gets a constant reference?

Read this article: Appearing and Disappearing consts in C++

Type deduction for auto variables in C++0x is essentially the same as for template parameters. (As far as I know, the only difference between the two is that the type of auto variables may be deduced from initializer lists, while the types of template parameters may not be.) Each of the following declarations therefore declare variables of type int (never const int):

auto a1 = i;
auto a2 = ci;
auto a3 = *pci;
auto a4 = pcs->i;

During type deduction for template parameters and auto variables, only top-level consts are removed. Given a function template taking a pointer or reference parameter, the constness of whatever is pointed or referred to is retained:

template<typename T>
void f(T& p);

int i;
const int ci = 0;
const int *pci = &i;

f(i);               // as before, calls f<int>, i.e., T is int
f(ci);              // now calls f<const int>, i.e., T is const int
f(*pci);            // also calls f<const int>, i.e., T is const int

This behavior is old news, applying as it does to both C++98 and C++03. The corresponding behavior for auto variables is, of course, new to C++0x:

auto& a1 = i;       // a1 is of type int&
auto& a2 = ci;      // a2 is of type const int&
auto& a3 = *pci;    // a3 is also of type const int&
auto& a4 = pcs->i;  // a4 is of type const int&, too

Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:

auto& my_foo2 = GetFoo();

Instead of having to specify it as const (same goes for volatile).

Edit: As for why auto deduces the return type of GetFoo() as a value instead of a reference (which was your main question, sorry), consider this:

const Foo my_foo = GetFoo();

The above will create a copy, since my_foo is a value. If auto were to return an lvalue reference, the above wouldn't be possible.

Using C++17 Parallel Algorithms for Better Performance--Billy O’Neal

Are you using the parallel capacities of the std?

Using C++17 Parallel Algorithms for Better Performance

by Billy O’Neal

From the article:

C++17 added support for parallel algorithms to the standard library, to help programs take advantage of parallel execution for improved performance. MSVC first added experimental support for some algorithms in 15.5, and the experimental tag was removed in 15.7.

The interface described in the standard for the parallel algorithms doesn’t say exactly how a given workload is to be parallelized. In particular, the interface is intended to express parallelism in a general form that works for heterogeneous machines, allowing SIMD parallelism like that exposed by SSE, AVX, or NEON, vector “lanes” like that exposed in GPU programming models, and traditional threaded parallelism.

Our parallel algorithms implementation currently relies entirely on library support, not on special support from the compiler. This means our implementation will work with any tool currently consuming our standard library, not just MSVC’s compiler. In particular, we test that it works with Clang/LLVM and the version of EDG that powers Intellisense...

How To Use std::visit With Multiple Variants--Bartlomiej Filipek

Variant utility.

How To Use std::visit With Multiple Variants

by Bartlomiej Filipek

From the article:

std::visit is a powerful utility that allows you to call a function over a currently active type in std::variant. It does some magic to select the proper overload, and what’s more, it can support many variants at once.

Let’s have a look at a few examples of how to use this functionality...