Inside STL: The shared_ptr constructor vs make_shared -- Raymond Chen

RaymondChen_5in-150x150.jpgThis article discusses the memory layouts when creating an object controlled by a shared_ptr in C++ through two methods: using a raw pointer and via the make_shared function. 

Inside STL: The shared_ptr constructor vs make_shared

By Raymond Chen

From the article:

There are two ways to create a new object that is controlled by a shared_ptr.

// From a raw pointer
auto p = std::shared_ptr<S>(new S());

// Via make_shared
auto p = std::make_shared<S>();

They result in two different memory layouts.

In the first case, you manually created a new S object, and then passed a pointer to it to the shared_ptr constructor. The shared_ptr adopts the raw pointer and creates a control block to monitor its lifetime. When the last shared pointer destructs, the Dispose() method deletes the pointer you passed in.¹ When the last shared or weak pointer destructs, the Delete() method deletes the control block.

SObjectizer Tales - Prelude--Marco Arena

A new blog series about SObjectizer and message passing:

SObjectizer Tales - Prelude

by Marco Arena

From the article:

Some time ago, I was looking for alternatives to Microsoft’s Asynchronous Agents Library. I developed a Windows application interfacing with industrial cameras and performing multiple tasks such as visualization and object recognition...

2023-09 Mailing Available

The 2023-09 mailing of new standards papers is now available.

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup
P0843R9 inplace_vector Gonzalo Brito Gadeschi 2023-09-14 2023-09 P0843R8 LEWG Library Evolution,LWG Library
P1068R9 Vector API for random number generation Ilya Burylov 2023-09-14 2023-09 P1068R8 LEWG Library Evolution
P1255R10 A view of 0 or 1 elements: views::maybe Steve Downey 2023-09-14 2023-09 P1255R9 SG9 Ranges,LEWG Library Evolution
P2264R5 Make assert() macro user friendly for C and C++ Peter Sommerlad 2023-09-13 2023-09 P2264R4 LEWG Library Evolution,LWG Library
P2542R4 views::concat Hui Xie 2023-09-11 2023-09 P2542R3 SG9 Ranges,LEWG Library Evolution
P2542R5 views::concat Hui Xie 2023-09-12 2023-09 P2542R4 SG9 Ranges,LEWG Library Evolution,LWG Library
P2686R2 constexpr structured bindings and references to constexpr variables Corentin Jabot 2023-09-14 2023-09 P2686R1 EWG Evolution,CWG Core
P2748R2 Disallow Binding a Returned Glvalue to a Temporary Brian Bi 2023-09-14 2023-09 P2748R1 EWG Evolution
P2755R0 A Bold Plan for a Complete Contracts Facility Joshua Berne, Jake Fevold, John Lakos 2023-09-13 2023-09   SG21 Contracts
P2760R0 A Plan for C++26 Ranges Barry Revzin 2023-09-17 2023-09   SG9 Ranges,LEWG Library Evolution
P2762R1 Sender/Receiver Interface For Networking Dietmar Kuhl 2023-09-15 2023-09 P2762R0 SG4 Networking,LEWG Library Evolution
P2833R2 Freestanding Library: inout expected span Ben Craig 2023-09-13 2023-09 P2833R1 LWG Library
P2846R1 size_hint: Eagerly reserving memory for not-quite-sized lazy ranges Corentin Jabot 2023-09-15 2023-09 P2846R0 LEWG Library Evolution
P2865R3 Remove Deprecated Array Comparisons from C++26 Alisdair Meredith 2023-09-14 2023-09 P2865R2 SG22 Compatability,CWG Core
P2866R1 Remove Deprecated Volatile Features From C++26 Alisdair Meredith 2023-09-15 2023-09 P2866R0 SG1 Concurrency and Parallelism,SG22 Compatability,LEWG Library Evolution
P2867R1 Remove Deprecated strstreams From C++26 Alisdair Meredith 2023-09-15 2023-09 P2867R0 LEWG Library Evolution
P2868R2 Remove Deprecated `std::allocator` Typedef From C++26 Alisdair Meredith 2023-09-14 2023-09 P2868R1 LEWG Library Evolution
P2869R2 Remove Deprecated `shared_ptr` Atomic Access APIs From C++26 Alisdair Meredith 2023-09-15 2023-09 P2869R1 LEWG Library Evolution
P2870R2 Remove `basic_string::reserve()` From C++26 Alisdair Meredith 2023-09-14 2023-09 P2870R1 LEWG Library Evolution
P2871R2 Remove Deprecated Unicode Conversion Facets From C++26 Alisdair Meredith 2023-09-15 2023-09 P2871R1 LEWG Library Evolution
P2872R2 Remove `wstring_convert` From C++26 Alisdair Meredith 2023-09-14 2023-09 P2872R1 LEWG Library Evolution
P2875R2 Undeprecate `polymorphic_allocator::destroy` For C++26 Alisdair Meredith 2023-09-15 2023-09 P2875R1 LEWG Library Evolution
P2885R2 Requirements for a Contracts syntax Timur Doumler 2023-08-29 2023-09 P2885R1 SG21 Contracts,EWG Evolution
P2909R1 Fix formatting of code units as integers (Dude, where's my char?) Victor Zverovich 2023-09-05 2023-09 P2909R0 LEWG Library Evolution
P2909R2 Fix formatting of code units as integers (Dude, where's my char?) Victor Zverovich 2023-09-16 2023-09 P2909R1 LWG Library
P2932R0 A Principled Approach to Open Design Questions for Contracts Joshua Berne 2023-09-13 2023-09   SG21 Contracts
P2935R1 An Attribute-Like Syntax for Contracts Joshua Berne 2023-09-15 2023-09 P2935R0 SG21 Contracts
P2935R2 An Attribute-Like Syntax for Contracts Joshua Berne 2023-09-15 2023-09 P2935R1 SG21 Contracts
P2944R2 Comparisons for reference_wrapper Barry Revzin 2023-09-17 2023-09 P2944R1 LEWG Library Evolution
P2951R3 Shadowing is good for safety Jarrad J. Waterloo 2023-09-02 2023-09 P2951R2 SG23 Safety and Security
P2955R1 Safer Range Access Jarrad J. Waterloo 2023-09-02 2023-09 P2955R0 LEWGI SG18: LEWG Incubator,SG23 Safety and Security
P2961R0 A natural syntax for Contracts Jens Maurer 2023-09-17 2023-09   SG21 Contracts
P2963R0 Ordering of constraints involving fold expressions Corentin Jabot 2023-09-15 2023-09   EWG Evolution
P2966R0 Making C++ Better for Game Developers -- Progress Report Patrice Roy 2023-09-05 2023-09   SG14 Low Latency,EWG Evolution
P2966R1 Making C++ Better for Game Developers -- Progress Report Patrice Roy 2023-09-13 2023-09 P2966R0 SG14 Low Latency,EWG Evolution
P2968R0 Make std::ignore a first-class object Peter Sommerlad 2023-09-07 2023-09   LEWG Library Evolution,LWG Library
P2971R0 Implication for C++ Walter E Brown 2023-09-14 2023-09   EWG Evolution
P2972R0 2023-09 Library Evolution Polls Inbal Levi 2023-09-17 2023-09   LEWG Library Evolution,LWG Library
P2973R0 Erroneous behaviour for missing return from assignment Thomas Köppe 2023-09-15 2023-09   SG12 Undefined and Unspecified Behavior,SG23 Safety and Security,EWG Evolution,CWG Core
P2976R0 Freestanding Library: algorithm, numeric, and random Ben Craig 2023-09-17 2023-09   LEWG Library Evolution

Inside STL: Smart Pointers -- Raymond Chen

RaymondChen_5in-150x150.jpgThe C++ standard library comes with a few smart pointer types. The simplest one is unique_ptr: This class babysits a raw pointer and remembers to delete it at destruction (in an appropriate manner). Dumping the contents of a unique_ptr is just looking at the raw pointer inside.

Inside STL: Smart Pointers

By Raymond Chen

From the article:

The C++ standard library comes with a few smart pointer types.

The simplest one is unique_ptr: This class babysits a raw pointer and remembers to delete it at destruction (in an appropriate manner). Dumping the contents of a unique_ptr is just looking at the raw pointer inside.

The complication is that there is also a deleter object in the unique_ptr. This deleter object is usually an empty class, so it is stored as part of a compressed pair.

The Visual Studio debugger has a visualizer that understands unique_ptr, but here’s what it looks like at a low level in the Microsoft implementation:

0:000< ?? p
class std::unique_ptr<S,std::default_delete<S> >
   +0x000 _Mypair          : std::_Compressed_pair<std::default_delete<S>,S *,1>
0:000< ?? p._Mypair
class std::_Compressed_pair<std::default_delete<S>,S *,1>
   +0x000 _Myval2          : 0x0000020a`11f08490 S
0:000< ?? p._Mypair._Myval2
struct S * 0x0000020a`11f08490 ← here is the unique object
   +0x000 a                : 0n42

CppCon 2023 Back to Basics: Algorithms -- Klaus Iglberger

Registration 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 some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2023!

Back to Basics: Algorithms

Tuesday, October 3 • 09:00 - 10:00

by Klaus Iglberger

Summary of the talk:

“There was never any question that the [standard template] library represented a breakthrough in efficient and extensible design” (Scott Meyers, Effective STL, 2008)

Originally developed as part of the Standard Template Library (STL), algorithms have become a must-know tool for every C++ developer. They increase productivity, significantly reduce bugs, and improve maintainability. This talk explains why and how algorithms do this. Additionally, it demonstrates why they are an amazing example for good, extensible software design.

CppCon 2023 A Fast, Compliant JSON Pull Parser for Writing Robust Applications -- Jonathan Müller

Registration 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 some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2023!

Express Your Expectations: A Fast, Compliant JSON Pull Parser for Writing Robust Applications

Tuesday, October 3 • 09:00 - 10:00

by Jonathan Müller

Summary of the talk:

There are, by now, several well-established C++ JSON libraries, for example, boost.JSON, rapidjson, and simdjson. C++ developers can choose between DOM parsers, SAX parsers, and pull parsers. DOM parsers are by design slow and use a lot of memory, SAX parsers are clumsy to use and the only well-known pull parser simdjson does not fully validate JSON documents and also has high non-constant memory usage. Our open-source JSON parser fills the gap between the existing parser libraries. It is a fully validating, fast, pull parser with O(1) memory usage.

Its main contribution, however, is the API design. All existing parsers verify that a parsed document is valid JSON. But most applications require the data to have a specific structure, for example, that an object has specific required keys while other keys may be optional. Their associated values in turn are expected to be, for example, strings, objects or arrays. Currently, developers need to implement their own checks and their own error handling on top of the existing parser APIs.

Our API forces developers to express these semantical constraints, providing automatic error handling in return. The resulting code concisely documents the required JSON structure and always handles errors correctly. We have found this to be extremely useful in practice.

This talk will show the JSON parser API in practice, compare it to the established parsers, and will demonstrate some elegant generic programming C++ techniques to beginners and intermediate C++ developers.

CppCon 2023 The Story on the Misuse of Exceptions and How to Do Better -- Peter Muldoon

Registration 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 some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2023!

Exceptionally Bad: The Story on the Misuse of Exceptions and How to Do Better

Tuesday, October 3 • 09:00 - 10:00

by Peter Muldoon

Summary of the talk:

Exceptions were originally heralded as a new modern way to handle errors. However the C++ community is split as to whether exceptions are useful or should be banned outright. It has not helped the pro-exception lobby that in their enthusiasm to embrace exceptions, a lot of code has been written that puts exceptions in a bad light.

In this talk, We will present the original intent/history of exceptions and a brief overview of how exception mechanics work and how they circumvent the usual stack return mechanism to set the stage. we will then examine the philosophy of using exceptions and then the many cases of exception misuse including resource management, retries, hierarchies, data passing and control flow to name but a few.

For each case, we will then suggest better ways to handle each specific situation. In many cases, exceptions are often dropped in favor of some other more appropriate paradigm.

Finally, we will introduce situations that can truly benefit from exceptions and what a model exception class might look like.

CppCon 2023 Powered by AI: A Cambrian Explosion for C++ Software Development Tools -- Emery Berger

Registration 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 some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2023!

Powered by AI: A Cambrian Explosion for C++ Software Development Tools

Thursday, October 5 • 09:35 - 10:05

by Emery Berger

Summary of the talk:

Large language models like GPT-4 are achieving state of the art results in a wide variety of well-studied areas, eclipsing past work in well-studied areas like auto-completion. I argue that they should also presage a "Cambrian explosion" – a wave of radically new kinds of software development tools, all powered by AI, that will make all our lives easier. This talk will focus on several tools of particular interest to C++ developers. First, as any C++ developer knows, C++ compiler errors are notoriously verbose and can be hard to decode even for experts. To address this challenge, we created CWhy, a compiler wrapper that translates C or C++ compiler errors into readable explanations, and even proposes potential fixes. Beyond compile-time errors, C++ developers must also spend considerable time in debuggers (like `gdb` or `lldb`) trying to track down the root cause of runtime errors. To simplify this task, we created ChatDBG, a system that augments debuggers with a new command you can run for post-mortem debugging: `why`. This command performs a root cause analysis of the issue leading to the error (e.g., a segfault) and proposes a fix. For both of these projects, we share some details about their internal workings, describe how they can effectively leverage large language models, and present examples of them in action.

CppCon 2023 C++20 Modules: The Packaging and Binary Redistribution Story -- Luis Caro Campos

Registration 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 some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2023!

C++20 Modules: The Packaging and Binary Redistribution Story

Monday, October 2 • 16:45 - 17:45

by Luis Caro Campos

Summary of the talk:

C++ modules are one of the most talked-about features introduced in C++20, but are still not widely in use. One of the biggest promises of C++ modules is to reduce overall compilation times by removing the need to have the compiler parsing the same include files across a big number of translation units. However, despite available compiler implementations, modules are still not generally in use. Why is this?

When put into practice, C++ modules introduce a dependency in the order in which translation units are built: in order to build a source file that contains a module import statement, the source file that exports the name module needs to have been compiled into a binary module interface beforehand. This calls for compilers and build systems to work together to correctly derive the order in which source files need to be compiled.

To overcome this, paper P1689 has been proposed for compilers to report the dependencies between source files, so that build tools (e.g. CMake and Ninja) can use this information to derive the correct build order. However, the currently available implementations have limitations and where all the source files that export module definitions should be visible by the same build.

How does this work in scenarios where we have dependencies (e.g. third-party) that are provided externally to the current build system? We have become accustomed to a model where a “binary” package contains, at the very least, header files (.h/.hpp) for the compiler, and libraries (.a/.so/.lib/.dylib) for the linker. How do modules fit into this model when it comes to installing and distributing prebuilt binaries? Now more files are required on the consuming side: the compiled binary module interface, and the corresponding module interface source file.

This talk will review the current state of modules in C++, by presenting the experience currently provided by the most recent versions of the relevant tools. Special focus will be put on the potential of using C++20 modules for external library dependencies, and how far we are from being able to consume external libraries using modules.

Inside STL: The array -- Raymond Chen

RaymondChen_5in-150x150.jpgThe C++ standard library array is just a C-style array wrapped inside a class so that it behaves like a normal C++ object instead of a wacky thing that undergoes decay.

Inside STL: The array

By Raymond Chen

From the article:

template<typename T, size_t N>
class array
{
    T elements[N];
};

template<typename T>
class array<T, 0>
{
};

The only weird case is N = 0. You are allowed to create a zero-length std::array, but C++ does not allow zero-length C-style arrays. The zero-length std::array is just an empty class.

Visual Studio and the Windows debugger come with a visualizer:

0:000> dx a
a                : { size=5 } [Type: std::array<int,5>]
    [<Raw View>]     [Type: std::array<int,5>]
    [0]              : 3 [Type: int]
    [1]              : 1 [Type: int]
    [2]              : 4 [Type: int]
    [3]              : 1 [Type: int]
    [4]              : 5 [Type: int]