C++23: The Next C++ Standard -- Rainer Grimm

TimelineCpp.pngC++23 will be the next C++ standard after C++20. This new standard significantly improves C++ but is less game-changing than C++98, C++11, or C++20. C++23 is more in the tradition of C++17.

C++23: The Next C++ Standard

By Rainer Grimm

From the article:

The C++ Standards

C++ is more than 40 years old. What happened in the last years? Here is a simplified answer ending in C++23.

C++98

At the end of the 80ths, Bjarne Stroustrup and Margaret A. Ellis wrote their famous book Annotated C++ Reference Manual (ARM). These books served two purposes. First, there were many independent C++ implementations. ARM defined, therefore, the functionality of C++. Second, ARM was the base for the first C++ standard: C++98 (ISO/IEC 14882). C++98 had a few essential features: templates, the standard template library (STL) with its containers and algorithms, strings, and IO streams.

C++03

With C++03 (14882:2003), C++98 got a technical correction that is so small that there is no place on my timeline. In the community, C++03, which includes C++98, is called legacy C++.

CppCon 2022 Back to Basics: Debugging in C++ -- Mike Shah

mikeshah-debugging.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!

Back to Basics: Debugging in C++

by Mike Shah

Summary of the talk:

I always tell my students, the debugger is your 'get out of jail free card' when working on a project. I say the same thing to professionals, debuggers are your 'get out of free jail card'. The reality is that programmers spend the majority of their time debugging as opposed to writing new code. Unfortunately many programmers do not learn how to use a debugger, or otherwise how they should approach debugging. In this talk I am going to show you how to debug C++ code, starting from the very basics and then demonstrating how a debugger like GDB can be used to help you track errors in CPU code. Attendees at this talk will learn names of debugging techniques (e.g. delta debugging), and I will demonstrate several debugging tools (stepping through code, capturing backtraces, conditional breakpoints, scripting, and even time traveling!) to demonstrate the power of debuggers. This is a beginner friendly talk where we are going to start from the beginning, but I suspect I may show a trick or two that folks with prior experience will appreciate.

Constrain your user-defined conversions -- Jonathan Mueller

jonathanmueller.pngSometimes you want to add an implicit conversion to a type. This can be done by adding an implicit conversion operator. For example, std::string is implicitly convertible to std::string_view.

Constrain Your User-Defined Conversions

By Jonathan Mueller

From the article:

Sometimes you want to add an implicit conversion to a type. This can be done by adding an implicit conversion operator. For example, std::string is implicitly convertible to std::string_view:

	class string { // template omitted for simplicity
	public:
	    operator std::string_view() const noexcept
	    {
	       return std::string_view(c_str(), size());
	    }
	}; 

The conversion is safe, cheap, and std::string and std::string_view represent the same platonic value — we match Tony van Eerd’s criteria for implicit conversions and using implicit conversions is justified.

However, even when all criteria are fulfilled, the conversion can still be dangerous.

Thread-Safe Queue - Two Serious Errors -- Rainer Grimm

concurrentarchitecture-grimm.pngIn my last post "Monitor Object"  I implemented a thread-safe queue. I made two serious errors. Sorry. Today, I will fix these issues.

Thread-Safe Queue - Two Serious Errors

by Rainer Grimm

From the article:

First, I want to show you again the erroneous implementation from my last post to understand the context.

// monitorObject.cpp

#include <condition_variable>
#include <functional>
#include <queue>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>

class Monitor {
public:
    void lock() const {
        monitMutex.lock();
    }

    void unlock() const {
        monitMutex.unlock();
    }

    void notify_one() const noexcept {
        monitCond.notify_one();
    }

    template <typename Predicate>
    void wait(Predicate pred) const {                 // (10)
        std::unique_lock<std::mutex> monitLock(monitMutex);
        monitCond.wait(monitLock, pred);
    }
  
private:
    mutable std::mutex monitMutex;
    mutable std::condition_variable monitCond;
};

template <typename T>                                  // (1)
class ThreadSafeQueue: public Monitor {
public:
    void add(T val){
        lock();
        myQueue.push(val);                             // (6)
        unlock();
        notify_one();
    }
  
    T get(){
        wait( [this] { return ! myQueue.empty(); } );  // (2)
        lock();
        auto val = myQueue.front();                    // (4)
        myQueue.pop();                                 // (5)
        unlock();
        return val;
    }

private:
    std::queue<T> myQueue;                            // (3)
};


class Dice {
public:
    int operator()(){ return rand(); }
private:
    std::function<int()> rand = std::bind(std::uniform_int_distribution<>(1, 6),
                                          std::default_random_engine());
};


int main(){
  
    std::cout << '\n';
  
    constexpr auto NumberThreads = 100;
  
    ThreadSafeQueue<int> safeQueue;                      // (7)

    auto addLambda = [&safeQueue](int val){ safeQueue.add(val);          // (8)
                                            std::cout << val << " "
                                            << std::this_thread::get_id() << "; ";
                                          };
    auto getLambda = [&safeQueue]{ safeQueue.get(); };  // (9)

    std::vector<std::thread> addThreads(NumberThreads);
    Dice dice;
    for (auto& thr: addThreads) thr = std::thread(addLambda, dice());

    std::vector<std::thread> getThreads(NumberThreads);
    for (auto& thr: getThreads) thr = std::thread(getLambda);

    for (auto& thr: addThreads) thr.join();
    for (auto& thr: getThreads) thr.join();
  
    std::cout << "\n\n";
   
}

CppCon 2022 C++ Lambda Idioms -- Timur Doumler

timurdoumler-lambdaidioms.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!

C++ Lambda Idioms

by Timur Doumler

Summary of the talk:

Ever since they were introduced in C++11, lambdas have been an essential language feature. Each subsequent standard added more functionality: we got generic lambdas and init captures in C++14, constexpr lambdas in C++17, default-construction and assignment, explicit template arguments and more in C++20, and even more possibilities are coming in the upcoming C++23 standard.

In this talk, we are looking at various interesting things you can do with lambdas. Some of these are well-established, useful idioms; others are lesser known, surprising tricks. Have you ever inherited from a lambda? Can you think of three different ways to call a lambda recursively? Do you know what happens if we assign an immediately-invoked lambda expression to a static variable? If not, then this talk is for you.

Object Ownership -- Ilya Doroshenko

objectownership-doroshenko.pngThis article goes over the spicy topic of object ownership. We covered the lifetime quirks, and we found out that manual memory management can be a nightmare, we new and delete in the correct order. There must be something better than that. Well, there is but it comes with its own can of worms.

Object Ownership

By Ilya Doroshenko

From the article:

Since we know the rules of new and delete, namely new allocates and delete destroys, we never really cared about who is responsible for the object. This caused a lot of confusion in the past. For instance, some API codes from Win32 return strings that should be LocalFree()d, like FormatMessage or GetEnvironmentStrings. POSIX, on the other hand, has strdup as a common example of you should free it yourself. This model is confusing because you may have a lot of return statements, before which you should always call free or delete, depending on the operation. However, we have RAII since the very beginning of C++, which adds constructors and destructors. So, in 1998 resourceful people decided to add auto_ptr to the standard.

The premise was simple:

  • a simple explicit constructor, that took raw pointer from new
  • destroyed by either an explicit release/reset or a destructor on the end of the block

This was the first attempt at a structured cleanup. As time passed, the initial point began to crumble. More issues arose and the question came up: Who owns the data?

 

 

CppCon 2022 C++ Coding with Neovim -- Prateek Raman

prateekraman-neovim.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!

C++ Coding with Neovim

by Prateek Raman

Summary of the talk:

If the command line has ever piqued your interest, specially using Vim/Neovim for C++ coding, but you have some reservations about losing access to IDE like features, wonder no more. This talk will present a productive C++ development environment on the Command Line using Neovim and LSP. LSP (Language Server Protocol) is the protocol which powers semantic auto-completion and intellisense in modern IDEs and we'll learn how Neovim's built-in LSP client integrates seamlessly with language servers to provide a rich editing experience.

Neovim is a modern editor built on top off the giant shoulders of Vim, and lets us have all the speed and efficiency of Vim as we edit code, while also providing rich support for C++ coding with intellisense and auto-completion via LSP. Along the way we'll also demonstrate Neovim's rich LSP aware plugin ecosystem for project navigation and IDE like workflows, all on the command line.

Even if you're comfortable with Vim/Neovim, but would like to learn how to level up the experience with modern IDE features, you will find this talk interesting.

Parsing Time Stamps Faster with SIMD Instructions -- Daniel Lemire

In software, it is common to represent time as a time-stamp string. It is usually specified by a time format string. Some standards use the format %Y%m%d%H%M%S meaning that we print the year, the month, the day, the hours, the minutes and the seconds. The current time as I write this blog post would be 20230701205436 as a time stamp in this format. It is convenient because it is short, easy to read and if you sort the strings lexicographically, you also sort them chronologically.

Parsing Time Stamps Faster with SIMD Instructions

by Daniel Lemire

From the article:

You can generate time stamps using any programming language. In C, the following program will print the current time (universal, not local time):

We are interested in the problem of parsing these strings. In practice, this means that we want to ...

 

AddressSanitizer continue_on_error -- Jim Radigan

jim-300x300.jpgVisual Studio 17.6 comes with new functionality in the Address Sanitizer runtime which provides a new “checked build” for C and C++. This new runtime mode diagnoses and reports hidden memory safety errors, with zero false positives, as your app runs.

AddressSanitizer continue_on_error

by Jim Radigan

From the article:

C++ memory safety errors are a top concern for the industry. In Visual Studio 17.6, we deliver a new experimental Address Sanitizer feature: continue_on_error (COE). We’ll remove the experimental label in 17.8. You compile as before, by simply adding the compiler flag -fsanitizer=address. With 17.6 you can enable the COE functionality by setting environment variables from the command line.

CppCon 2022 C++23 - What's In It For You? -- Marc Gregoire

marcgregoire-wiify.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!

C++23 - What's In It For You?

by Marc Gregoire

Summary of the talk:

C++23, the next release of the C++ standard, is scheduled to be released in 2023. The update introduces new features to the core language and to the Standard Library. If you want to stay up to date with the latest features that are upcoming with this new release, then this session is for you.

The session includes core language topics such as consteval if statements, multidimensional subscript operators, decay copy, unreachable code, and more. New Standard Library features that will be shown include monadic operations for std::optional, std::flat_map, std::flat_set, a stacktrace library, changes to the ranges library, improvements to std::format, std::expected, and many more.

The session will include references to other sessions at CppCon, if applicable, for more deep-dive information on any particular topic.