An Extraterrestrial Guide to C++ Formatting--Victor Zverovich

The future of now.

An Extraterrestrial Guide to C++ Formatting

by Victor Zverovich

From the article:

Consider the following use case: you are developing the Enteropia[2]-first Sepulka[3]-as-a-Service (SaaS) platform and have a server code written in C++ that checks the value of sepulka’s squishiness received over the wire and, if the value is invalid, logs it and returns an error to the client. Squishiness is passed as a single byte and you want to format it as a 2-digit hexadecimal integer, because that is, of course, the Ardrite[1] National Standards Institute (ANSI) standard representation of squishiness. Let’s implement the logging part using different formatting facilities provided by C++...

Quick Q: Is it useful to pass std::weak_ptr to functions?

Quick A: yes

Recently on SO:

Is it useful to pass std::weak_ptr to functions?

Consider this toy example.

struct PointerObserver
{
    std::weak_ptr<int> held_pointer;

    void observe( std::weak_ptr<int> p )
    {
        held_pointer = std::move(p);
    }

    void report() const
    {
        if ( auto sp = held_pointer.lock() )
        {
            std::cout << "Pointer points to " << *sp << "\n";
        }
        else
        {
            std::cout << "Pointer has expired.\n";
        }
    }
};

In this example, a function observe holds state.

Its weak_ptr parameter communicates that this smart pointer is not owning, but reserves the ability to own at a later time, safely detecting if the pointer has expired.

How to build a JIT compiler in C++ with LLVM -- Mark Leone

At the December 2018 meetup of Utah C++ Programmers, Mark Leone will give us a presentation on how to build a JIT compiler in C++ with LLVM. Food will be provided, so please RSVP so we have a proper headcount.

How to build a JIT compiler in C++ with LLVM

by Mark Leone

About the meetup:

In this talk I'll show how to compile a simple programming language into machine code using the LLVM compiler toolkit. I'll start with a simple lexer (using re2c) that converts a stream of characters into a stream of tokens (e.g. numbers and identifiers), followed by a simple parser (using recursive descent) that produces a syntax tree. Then I'll show how to generate LLVM intermediate code (IR), optimize it, and generate machine code using the LLVM JIT engine. Full source code will be available afterwards.

No compiler experience will be required, although a reasonable familiarity with C++ will be assumed. Check out the LLVM Tutorial for a preview of similar material.

About the speaker: Mark Leone has been working with C++ as a graphics software engineer for 20 years. He recently joined the OptiX ray tracing team at NVIDIA here in Salt Lake City. He has over two dozen movie credits for his previous work at Pixar and Weta Digital (in New Zealand).

How to optimize C and C++ code in 2018--Iurii Krasnoshchok

Are you aware?

How to optimize C and C++ code in 2018

by Iurii Krasnoshchok

From the article:

We are still limited by our current hardware. There are numerous areas where it just not good enough: neural networks and virtual reality to name a few. There are plenty of devices where battery life is crucial, and we must count every single CPU tick. Even when we’re talking about clouds and microservices and lambdas, there are enormous data centers that consume vast amounts of electricity.

Even boring tests routine may quietly start to take 5 hours to run. And this is tricky. Program performance doesn‘t matter, only until it does.

A modern way to squeeze performance out of silicon is to make hardware more and more sophisticated...

Trip Report: C++ Standards Meeting in San Diego, November 2018--Botond Ballo

New trip report.

Trip Report: C++ Standards Meeting in San Diego, November 2018

by Botond Ballo

From the article:

A few weeks ago I attended a meeting of the ISO C++ Standards Committee (also known as WG21) in San Diego, California. This was the third committee meeting in 2018; you can find my reports on preceding meetings here (June 2018, Rapperswil) and here (March 2018, Jacksonville), and earlier ones linked from those. These reports, particularly the Rapperswil one, provide useful context for this post...

How to Design Function Parameters That Make Interfaces Easier to Use (3/3)--Jonathan Boccara

The end.

How to Design Function Parameters That Make Interfaces Easier to Use (3/3)

by Jonathan Boccara

From the article:

This is the final article in the series about function parameters. This series contains:

  • Part 1: interface-level parameters, one-parameter functions, const parameters,
  • Part 2: calling contexts, strong types, parameters order,
  • Part 3: packing parameters, processes, levels of abstraction.

Exploring C++20 - Designated initialisers--Tobias Widlund

Will be very useful.

Exploring C++20 - Designated initialisers

by Tobias Widlund

From the article:

Compared to many other C++20 additions this one is quite small, only needing a single box of explanation on cppreference. Despite this, I quite like the feature as it helps code that uses aggregate initialisation to be more readable and less error prone - both are well needed in that area!

Quick Q: Type inference of 'auto' when using range-for loop in multidimensional arrays

Quick A: a pointer has no size information.

Recently on SO:

Type inference of 'auto' when using range-for loop in multidimensional arrays

The type of row is deduced to be int*. That means, just like any other int*, the compiler doesn't know how big the array it points to is, or even that it's a pointer to the first element of an array at all. All of that information is lost when an array decays into a pointer to its first element.

If instead you use something like

for (auto& row : ia) //<-- NOTE: row is now a reference
    for (int* j = std::begin(row); j != std::end(row); ++j)
        std::cout << *j << '\n';

then the type of row will be deduced to int (&)[4]: reference to an array of 4 ints. The length information is retained, so std::begin and std::end have the information they need.

PS: Just as a note: range-for works by using std::begin and std::end internally, so the above can be a bit more concisely written as

for (auto& row : ia)
    for (auto j : row)
        std::cout << j << '\n';