C++ Online

C++ Online is a brand new online C++ conference, including great value training workshops and regular talks, keynotes and lightning talks. The main conference runs from 29th February to 2nd March.

C++ Online, Revisited

From the article:

C++ Online also now has its own website, where you can find all the details, including speakers, workshops and ticket sales.
So go to cpponline.uk now!

The decorator pattern: From basic to advanced concepts in C++ -- John Farrier

The Decorator Pattern stands out for its unique ability to dynamically add new functionalities to objects without altering their structure.

The decorator pattern: From basic to advanced concepts in C++

John Farrier

From the article:

The Decorator Pattern allows developers to seamlessly decorate or wrap objects with new behaviors or responsibilities, ensuring that the enhancements are scalable, manageable, and, most importantly, interchangeable. This capability is crucial in an era where software requirements are constantly evolving, demanding adaptable and resilient systems to change.

Let’s illustrate this with a basic C++ example. Consider a window in a user interface as the Component. You want to add functionalities like scrollbars or a border without redesigning the window itself.

Here’s a simplified version of how you might implement this:

class Window { // Component
public:
    virtual void draw() = 0;
    virtual ~Window() {}
};

class SimpleWindow : public Window { // Concrete Component
public:
    void draw() override {
        // Draw the window
    }
};

class WindowDecorator : public Window { // Decorator
protected:
    Window* window;
public:
    WindowDecorator(Window* wnd) : window(wnd) {}
    void draw() override {
        window->draw(); // Delegate to the component
    }
};

class ScrollbarWindow : public WindowDecorator { // Concrete Decorator
public:
    ScrollbarWindow(Window* wnd) : WindowDecorator(wnd) {}
    void draw() override {
        WindowDecorator::draw(); // Draw the window
        drawScrollbar(); // Add scrollbar
    }
private:
    void drawScrollbar() {
        // Draw the scrollbar
    }
};


In this example, SimpleWindow is a Concrete Component that can be decorated with additional features. WindowDecorator is a Decorator that holds a reference to a Window object and delegates the draw operation to it. ScrollbarWindow is a Concrete Decorator that adds a scrollbar to the window.

CopperSpice: Generic vs Meta Programming

New video on the CopperSpice YouTube Channel:

Generic vs Meta Programming

by Barbara Geller and Ansel Sermersheim

About the video:

We released a new video which compares Generic and Meta Programming and how they are used in various computer languages like C++, Rust, and Go. In this video we also explain the definition of Template Meta Programming and when TMP is actually Reflection.

Simply using a template does not imply you are doing template meta programming and using a code generator does not mean you have implemented reflection.

Please take a look and remember to subscribe.

Subtle C++ Compiler Error with std::optional and the Conditional Operator -- Giovanni Dicanio

An example of writing clear code with good intention, but getting an unexpected C++ compiler error:

Subtle C++ Compiler Error with std::optional and the Conditional Operator

by Giovanni Dicanio

From the article:

I was asked: “What’s the problem here? Are there limitations of using the {} syntax to specify nothing?”

This is a good question. So, clearly, the C++ compiler didn’t interpret the {} syntax as a way to default-initialize the std::optional in case the string was not empty (i.e. the second “branch” in the conditional ternary operator).

A first step to help the C++ compiler figuring out the programmer’s intention could be to be more explicit. So, instead of using {}, you can try and use the std::nullopt constant, which represents an optional that doesn’t store any value. [...]

[...]

P.S. I’m not a C++ “language lawyer”, but it would be great if the C++ language could be extended to allow the original simple code to just work.

IEEE Floating Point and the Radar Range Equation in C++ -- John Farrier

The implementation of mathematical equations demands a keen awareness of the computational environment. This article uses a practical example of implementing the Radar Range Equation, a cornerstone formula in radar technology, to illustrate the importance of considering IEEE floating point representation in C++ for accurate and reliable computations.

Engineering Mathematics: A Focus on IEEE Floating Point and the Radar Range Equation in C++

by John Farrier

From the Article:

The nature of IEEE floating-point numbers in C++ brings certain challenges, especially when dealing with large or small numbers and operations like multiplication and division. Precision issues can significantly impact the accuracy of computations, making it essential to adapt the implementation strategy.

 

The Double Life of Objects -- Andrzej Krzemieński

doublelife-1.pngIn the world of C++, the concept of object lifetime and constness can become a bit hazy when copy elision, a popular optimization technique, comes into play. In this article, Andrzej Krzemieński explores the intricacies of object lifetimes and constness, using a class called Rng to illustrate how objects can appear const in one context and non-const in another due to copy elision. He'll also delve into the motivations behind C++'s handling of const objects and how it impacts program behavior.

The Double Life of Objects

by Andrzej Krzemieński

From the article:

Some common knowledge: the lifetime of the object starts when its initialization is complete. Based on this we can get some further expectations: an object becomes const only after its initialization is complete. But this lifetime property of objects becomes blurred when copy elision comes into play. When a copy is elided, we have a situation where we would otherwise have two objects, each initialized separately, but now they are blended into one, its life time spanning across the caller and the calle, which has a number of surprising effects, receiving two initializations being one of them.

In the following examples we will use class Rng that is quite small but convincing: you define a class when you need to maintain the invariant. Our class represents a range of integers between the minimum and maximum values.

doublelife-1.png

Now, let’s try to use it in a somewhat artificial program:

doublelife-2.png

2024-01 Mailing Available

The 2024-01 mailing of new standards papers is now available.

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup
P1255R11 A view of 0 or 1 elements: views::maybe Steve Downey 2024-01-12 2024-01 P1255R10 SG9 Ranges,LEWG Library Evolution
P1255R12 A view of 0 or 1 elements: views::maybe Steve Downey 2024-01-16 2024-01 P1255R11 SG9 Ranges,LEWG Library Evolution,LWG Library
P1709R5 Graph Library Phillip Ratzloff 2024-01-15 2024-01 P1709R4 SG6 Numerics,SG14 Low Latency,SG19 Machine Learning
P2019R5 Thread attributes Corentin Jabot 2024-01-13 2024-01 P2019R4 LEWG Library Evolution
P2527R3 std::variant_alternative_index and std::tuple_element_index Alex Christensen 2024-01-02 2024-01 P2527R2 LEWG Library Evolution
P2664R6 Proposal to extend std::simd with permutation API Daniel Towner 2024-01-16 2024-01 P2664R5 SG1 Concurrency and Parallelism,LEWG Library Evolution
P2748R3 Disallow Binding a Returned Glvalue to a Temporary Brian Bi 2024-01-08 2024-01 P2748R2 CWG Core
P2748R4 Disallow Binding a Returned Glvalue to a Temporary Brian Bi 2024-01-08 2024-01 P2748R3 CWG Core
P2835R2 Expose std::atomic_ref's object address Gonzalo Brito Gadeschi 2024-01-10 2024-01 P2835R1 LEWG Library Evolution
P2894R2 Constant evaluation of Contracts Timur Doumler 2024-01-11 2024-01 P2894R1 SG21 Contracts
P2900R4 Contracts for C++ Joshua Berne 2024-01-16 2024-01 P2900R3 SG21 Contracts
P2932R3 A Principled Approach to Open Design Questions for Contracts Joshua Berne 2024-01-15 2024-01 P2932R2 SG21 Contracts
P2946R1 A flexible solution to the problems of `noexcept` Pablo Halpern 2024-01-15 2024-01 P2946R0 EWGI SG17: EWG Incubator,EWG Evolution
P2957R1 Contracts and coroutines Andrzej Krzemieński 2024-01-13 2024-01 P2957R0 SG21 Contracts
P2963R1 Ordering of constraints involving fold expressions Corentin Jabot 2024-01-13 2024-01 P2963R0 EWG Evolution
P2988R1 std::optional<T&> Steve Downey 2024-01-05 2024-01 P2988R0 LEWG Library Evolution,LWG Library
P3044R0 sub-string_view from string Michael Florian Hava 2024-01-15 2024-01   LEWG Library Evolution
P3054R0 2023-12 Library Evolution Poll Outcomes Inbal Levi 2024-01-13 2024-01   All of WG21
P3079R0 Should ignore and observe exist for constant evaluation of contracts? Oliver Rosten 2024-01-11 2024-01   SG21 Contracts
P3084R0 Slides for LEWG views::maybe 20240109 Steve Downey 2024-01-12 2024-01   LEWG Library Evolution
P3086R0 Proxy: A Pointer-Semantics-Based Polymorphism Library Mingxin Wang 2024-01-16 2024-01   LEWGI SG18: LEWG Incubator,LEWG Library Evolution
P3087R0 Make direct-initialization for enumeration types at least as permissive as direct-list-initializatio Jan Schultke 2024-01-16 2024-01   LEWGI SG18: LEWG Incubator

Use SIMD: Save The Planet -- Andrew Drakeford

simddrake.pngWriting efficient code is challenging but worthwhile. Andrew Drakeford demonstrates how SIMD (Single Instruction Multiple Data) can reduce your carbon footprint.

Use SIMD: Save The Planet

by Andrew Drakeford

From the article:

Some sources claim that data centres consumed 2.9% of the world’s electricity in 2021 [Andrae15]. With the recent sharp increase in energy prices, many firms became aware of this cost. Additionally, trends in AI use and the near-exponential growth in both network and data services projected over the next few years [Jones18] suggest that this situation will only get worse.

A data centre’s single purpose is to run our software (not heat the planet). But what if the processing cores that our software runs on have a unique, often unused, feature that would enable them to do the work several times faster? Would this also mean several times more efficiently? This feature is SIMD (Single Instruction Multiple Data).

SIMD is a parallel computing technology that allows processors to perform the same operation on multiple data elements simultaneously. By using SIMD instructions, a single CPU instruction can operate on multiple data elements in a single clock cycle. For example, with 256-bit SIMD registers, it is possible to process eight 32-bit floating-point numbers or sixteen 16-bit integers in parallel. This leverages the data-level parallelism inherent in many algorithms, such as mathematical computations, image processing, audio processing, and simulations. Figure 1 illustrates the element-wise addition of two sets of numbers using the SSE2, AVX2 and AVX512 instructions sets.


 

SIMD instructions use vector registers, which can hold multiple data elements. These registers allow the CPU to apply a single instruction to all the elements simultaneously, thus reducing the instruction count. Consequently, SIMD can provide a substantial speedup for tasks that exhibit regular and parallelizable data processing patterns.