March 2021

CopperSpice: Mutex + Lock = CsLibGuarded

New video on the CopperSpice YouTube Channel:

Mutex + Lock = CsLibGuarded

by Barbara Geller and Ansel Sermersheim

About the video:

Our newest C++ video looks at a better way to lock a mutex. We look at making the data part of the solution. The video walks through several variations of an example to explain why the data, mutex, and lock should be tied together and the benefits that result from this change.

Please take a look and remember to subscribe!

Introduction to OpenCV with VcPkg and CMake -- Richard Thomson

Utah C++ Programmers has released a new video.

Introduction to OpenCV with VcPkg and CMake

by Richard Thomson

From the video description:

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.

This month, Richard Thomson will give us an introduction to OpenCV and walk us through some examples of using the library. OpenCV is a large library, so not every detail can be presented at once. However, we'll try to get a feel for what it looks like to use components from the library and perform some simple image manipulation tasks.

YouTube: https://www.youtube.com/watch?v=e7GtcsSJ51s

Don't automatically use auto parameters in C++ -- Lesley Lai

Should we use almost always auto parameters?

Don't automatically use auto parameters in C++

by Lesley Lai

From the article:

Since C++14, we can create lambda expressions that take auto parameters. C++20 generalizes this idea by allowing us to do the same thing for regular functions. With this feature's advent, the programming style where all parameters are auto becomes popular among some C++ programmers. However, I think we should not use it if we had to. The more specific the type of the parameter is, the better.

Example of How New Diagnostics Appear in PVS-Studio

Users sometimes ask how new diagnostics appear in the PVS-Studio static analyzer. We answer that we draw inspiration from a variety of sources: books, coding standards, our own mistakes, our users' emails, and others. Recently we came up with an interesting idea of a new diagnostic.

Example of How New Diagnostics Appear in PVS-Studio

by Andrey Karpov

From the article:

As for application software development, it doesn't make sense to enable them. The CovidSim project could do without them. Otherwise, a user will simply drown in a huge number of messages that are of little use in this case. For example, when experimenting with this set of diagnostics, we received more than a million warnings for some medium-sized open projects. Roughly speaking, every third line of code might be faulty in the view of MISRA. No one will scrape through all warnings, much less fix them. The project is either developed immediately taking into account MISRA recommendations, or this coding standard is irrelevant for it.
 

How C++ Resolves a Function Call--Jeff Preshing

Know everything about it.

How C++ Resolves a Function Call

by Jeff Preshing

from the article:

C is a simple language. You’re only allowed to have one function with each name. C++, on the other hand, gives you much more flexibility:

  • You can have multiple functions with the same name (overloading).
  • You can overload built-in operators like + and ==.
  • You can write function templates.
  • Namespaces help you avoid naming conflicts.

I like these C++ features. With these features, you can make str1 + str2 return the concatenation of two strings. You can have a pair of 2D points, and another pair of 3D points, and overload dot(a, b) to work with either type. You can have a bunch of array-like classes and write a single sort function template that works with all of them.

But when you take advantage of these features, it’s easy to push things too far. At some point, the compiler might unexpectedly reject your code with errors like:

error C2666: 'String::operator ==': 2 overloads have similar conversions
note: could be 'bool String::operator ==(const String &) const'
note: or       'built-in C++ operator==(const char *, const char *)'
note: while trying to match the argument list '(const String, const char *)'

Like many C++ programmers, I’ve struggled with such errors throughout my career. Each time it happened, I would usually scratch my head, search online for a better understanding, then change the code until it compiled. But more recently, while developing a new runtime library for Plywood, I was thwarted by such errors over and over again. It became clear that despite all my previous experience with C++, something was missing from my understanding and I didn’t know what it was.

Fortunately, it’s now 2021 and information about C++ is more comprehensive than ever. Thanks especially to cppreference.com, I now know what was missing from my understanding: a clear picture of the hidden algorithm that runs for every function call at compile time.

Creating other types of synchronization objects that can be used with co_await, part 6--Raymond Chen

The series continue.

Creating other types of synchronization objects that can be used with co_await, part 6

by Raymond Chen

From the article:

Our next stop in showing off our library for building awaitable synchronization objects is the semaphore. This will look very familiar because a semaphore with a maximum token count of 1 is the same thing as an auto-reset event, so we can just extend our auto-reset event implementation to support multiple tokens...

Non-Terminal Variadic Parameters and Default Values--Bartlomiej Filipek

How to best use source_location?

Non-Terminal Variadic Parameters and Default Values

by Bartlomiej Filipek

From the article:

Currently, as of C++20, there’s no support for so called non-terminal variadic arguments. For example, we cannot write:

template <class ...Args> void func(Args&& ...args, int num=42);
func(10, 20); // error

As you can see, I wanted 10 and 20 to be passed as ...args and 42 as a default value for num. Compilers currently cannot resolve this code.

In this blog post, I’d like to show you a couple of tricks you can implement to avoid this issue. Knowing those techniques might help with things like logging functions where we could have std::source_location at the end of a function declaration...