60 terrible tips for a C++ developer

In this article, you're going to find 60 terrible coding tips — and explanations of why they are terrible. It's a fun and serious piece at the same time. No matter how terrible these tips look, they aren't fiction, they are real: we saw them all in the real programming world.

60 terrible tips for a C++ developer

by Andrey Karpov

From the article:

Terrible tip N46. Write your code as if the chairman of the IOCCC judges will read it and as if they know where you live (to come and give you the prize).

It's a reference to a quote — "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live". This is John F. Woods's phrase, however, it's sometimes credited to Steve McConnell who quoted it in his "Code Complete" book.

Into the Extreme – Fold-Expressions -- Coral Kashri

CoralKashri.pngFold expressions exist in C++ since C++17 and significantly affect how we treat variadic templates. Back in the day, I wrote about fold-expressions as part of the metaprogramming series, but today we will explore the extreme cases of fold-expression usages.

Into the Extreme – Fold-Expressions

by Coral Kashri

From the article:

In the case of a unary fold (fold expression without initialization), this case is legal for 3 types of operators: &&||, and ,.

Operator &&

template <typename ...Args>
auto and_cond(Args... args) {
    return (args && ...);
}

In case of empty parameters (for the call and_cond()), the function will return true. A reasonable explanation for this decision might be that && operator requires there won’t be any part that evaluates false. In this case, there are no parts at all, so none of the parts evaluates false, and therefore the result should be...

CppCon 2022 What’s New in C++23 -- Sy Brand

cppcon-2022-whats-new-in-cpp23-sy-brand.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

What’s New in C++23

by Sy Brand

Summary of the talk:

C++23 comes with a host of language and library features to simplify your code, make it more expressive, and give you more power to play with. With the help of my cats, I’ll walk you through the majority of upcoming features, showing you how they can work together and what benefits you’ll gain from upgrading when the time comes.

Dealing with Mutation: Locking -- Rainer Grimm

dealingwithmutationlocking.pngLocking is a straightforward idea to protect a critical section. A critical section is a section of code that, at most, one thread can use at any time.

Dealing with Mutation: Locking

by Rainer Grimm

From the article:

Scoped locking is the idea of RAII applied to a mutex. Scoped locking is also known as synchronized block and guard. The key idea of this idiom is to bind the resource acquisition and release to an object’s lifetime. As the name suggests, the lifetime of the object is scoped. Scoped means that the C++ run time is responsible for object destruction and, therefore, for releasing the resource.

The class ScopedLock implements Scoped Locking.

CppCon 2022 The Future of C++ -- Neil Henderson

cppcon-2022-the-future-of-cpp-neil-henderson.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: The Future of C++

by Neil Henderson

Summary of the talk:

Hopefully a light-hearted, comedic and entertaining look at the future of C++ from an Australian perspective.

The Case of the Two Billion Characters Long String -- Giovanni Dicanio

Several times you need to pass data (including text) from cross-platform C++ code to platform-specific C++ code. If you don't pay enough attention, weird bugs can happen at the boundary.

The Case of the Two Billion Characters Long String

by Giovanni Dicanio

From the article:

What is going on here? What is the origin of this bug? Why is the string reported as being more than 2 billion characters long?

Let's try to solve this mystery, with a sprinkle of assembly language, too!

 

Storage duration and Non-local Objects in C++ -- Bartlomiej Filipek

Filipek-book.pngC++ allows us to declare various forms of non-local objects: they usually live throughout the execution of the whole program. In this article, we’ll look at global variables, dynamic, and thread-local objects. We’ll also consider new features for safe initialization C++20.

Storage duration and Non-local Objects in C++

by Bartlomiej Filipek

From the article:

To start, we need to understand two key properties of an object in C++: storage and linkage. Let’s begin with the definition of storage, from [basic.stc#general]:
"The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object."
An object in C++ has one of the following storage duration options:

CppCon 2022 Reflection in C++ - Past, Present, and Hopeful Future -- Andrei Alexandrescu

cppcon-2022-reflection-in-cpp-past-present-and-hopeful-future-andrei-alexan.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Reflection in C++ - Past, Present, and Hopeful Future

by Andrei Alexandrescu

Summary of the talk:

Aspect Oriented Programming. Metaobject protocols. Intentional programming. AspectC++. OpenC++. C++ metaclasses. Reflection and related technologies already has a long history in the theory and practice of several programming languages, including C++. However, the lofty promises of the 1990s (user-defined language semantics, infinite configurability, ultimate code reuse) failed to materialize in mainstream success.

Within the standard C++ realm, a reflection proposal has had a long and meandering road that is finally converging. Will it be successful? Why is this time different? And most importantly, what's in it for the community - what compelling applications are at the horizon to justify the addition to an already large language core?

This talk explores these questions and several related others. Although you won't take home code and insights that you can put to work tomorrow, you will acquire something that is less urgent but arguably more important: a vision of a better way of writing programs. Think generic programming without the pain, high-leverage generic code, seamless integration with foreign languages, and much more.

 

CppCon 2022 find-move-candidates in Cpp -- Chris Cotter

cppcon-2022-find-move-candidates-in-cpp-chris-cotter.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: find-move-candidates in C++

by Chris Cotter

Summary of the talk:
A clang tool to automatically find code that should use std::move to reduce unnecessary copies.

Bitwise Binary Search: Elegant and Fast -- Orson Peters

peters-bitwisebinarysearch.png

I recently read the article Beautiful Branchless Binary Search by Malte Skarupke. In it they discuss the merits of the following snippet of C++ code implementing a binary search.

Bitwise Binary Search: Elegant and Fast

by Orson Peters

From the article:

In this article I will provide an alternative implementation based on similar ideas but with a very different interpretation that is (in my opinion) incredibly elegant and clear to understand, at least as far as binary searches go. The resulting implementation also saves a comparison in almost every case and ends up quite a bit smaller.