August 2023

Inside STL: The vector -- Raymond Chen

RaymondChen_5in-150x150.jpgThe C++ language comes with a standard library, and although implementations are welcome to implement the library types in whatever manner they choose, they are constraints imposed by the standard which often force one of a small number of possible implementations.

Inside STL: The vector

By Raymond Chen

The std::vector is one of those types which is constrained to the point that there’s really only one viable implementation.

Internally, a std::vector basically looks like this:

template<typename T>
struct vector
{
    T* first;
    T* last;
    T* end;
};

The first is a pointer to the beginning of a single memory allocation that holds the vector contents.

The last is a pointer one past the end of the last valid vector element.

The end is a pointer one past the end of the last allocated memory for the vector.

The picture for this is as follows:

insidestlpic-chen.png

CppCon 2023 Customization Methods: Connecting User and Library Code -- Inbal Levi

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!

Customization Methods: Connecting User and Library Code

Monday, October 2 • 11:00am - 12:00pm

by Inbal Levi

Summary of the talk:

The interface between a library and its users is a major design consideration for every developer, and has been evolving greatly over C++’s lifetime. In this talk we will go over different methods for connecting generic library code with user-side specific code, commonly known as Customization Points. We will cover the benefits and downsides of different methods (including CTS, ADL, Concepts, CTOs, and the latest to be considered - tag_invoke), and present future directions for these mechanisms.

At the end of the talk, you’ll be familiar with the terminology and developments in this field, including the ones planned for C++23 and C++26.

CppCon 2023 Back to Basics: Templates -- Rainer Grimm

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: Templates

Monday, October 2 • 11:00am - 12:00pm

by Rainer Grimm

Summary of the talk:

The future of C++ speaks templates. Therefore, a professional C++ programmer must know the basics of templates. In this talk, I will provide this basic knowledge about function and class templates, template arguments and parameters, and template specialization. Additionally, thanks to C++ Insights, I can further deepen your intuition about templates.

Inside STL: The Pair and The Compressed Pair -- Raymond Chen

RaymondChen_5in-150x150.jpgThe C++ language comes with a standard library. When you’re debugging your C++ code, you may have to go digging inside the implementation to extract information from crash dumps. This mini-series is going to look at how various C++ standard library types are implemented by the three major implementations (clang, gcc, and msvc).

Inside STL: The Pair and The Compressed Pair

By Raymond Chen

From the article:

The C++ language comes with a standard library. When you’re debugging your C++ code, you may have to go digging inside the implementation to extract information from crash dumps. This mini-series is going to look at how various C++ standard library types are implemented by the three major implementations (clang, gcc, and msvc).

We’ll start with the lowly std::pair. Its definition is quite simple.

template<typename T1, typename T2>
struct pair
{
    T1 first;
    T2 second;
};

The names of the members of std::pair are required by the C++ language standard, so you don’t see any variation here. Here’s how it looks in the Windows debugger:

0:000> ?? t
struct std::pair<int,int>
   +0x000 first            : 0n42
   +0x004 second           : 0n99

CppCon 2023 Delivering Safe C++ -- Bjarne Stroustrup

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!

Delivering Safe C++

Monday, October 2 • 8:45am - 10:30am

by Bjarne Stroustrup

Summary of the talk:

Type safety was one of the key initial C++ design ideals. We have evolved C++ to the point where we can write C++ with no violations of the type system, no resource leaks, no memory corruption, no garbage collector, no limitation of expressiveness or performance degradation compared to well-written modern C++.

We face three major challenges: To define what “safe” means in the context of various C++ uses, to guarantee such safety where guarantees are needed, and to get developers to write such verified safe code.

I outline an approach based on safety profiles to address these challenges, describe an approach to eliminate dangling pointers, and suggest how to eliminate all dangling pointers and all range errors. My aim for key applications is verified type-and-resource-safe C++. An emphasis is on minimizing costly run-time checks through the use of abstractions. I see the current emphasis on safety as an opportunity to complete one aspect of C++’s fundamental aims in real-world code.

Understanding Ranges Views and View Adaptors Objects in C++20/C++23 -- Bartlomiej Filipek

rangesviews-filipek.pngIn this article, we’d shed some light on the implementation of ranges::reverse_view and std::views::reverse. We’ll compare them to understand the differences between views and their adaptor objects.

Understanding Ranges Views and View Adaptors Objects in C++20/C++23

By Bartlomiej Filipek

From the article:

Let’s look at an example to understand how these views work. Assume we have a range r of integers from 1 to 5. When we apply std::views::reverse to r, it creates a view representing the elements of r in the reverse order.
#include <ranges> 
#include <vector> 
#include <iostream>  

int main() {
  std::vector<int> r = {1, 2, 3, 4, 5};  
  auto reversed = r | std::views::reverse;  
  for (auto i : reversed)  
     std::cout << i << " ";   

  // same as:  
  for (auto i : r | std::views::reverse)  
     std::cout << i << " "; 
}

No, C++ static analysis does not have to be painful -- Geoffray Adde

Static analysis as part of your CI pipeline has long been possible -- but tedious, at best, to setup. Sonar are upending that with an innovative new approach.

No, C++ static analysis does not have to be painful

by Geoffray Adde

From the article:

It seems impossible to offer serious C++ static analysis with a great configuration experience. Users must suffer the pain, or not use it at all.
Well, at Sonar we believe in making the impossible, possible and so we've gone ahead and solved this once and for all!

Perfect Forwarding Forwards Objects Not Braced Things Trying To Become Objects -- Raymond Chen

RaymondChen_5in-150x150.jpgIn C++, perfect forwarding is the act of passing a function’s parameters to another function while preserving its reference category. It is commonly used by wrapper methods that want to pass their parameters through to another function, often a constructor.

Perfect Forwarding Forwards Objects, Not Braced Things That Are Trying To Become Objects

By Raymond Chen

From the article:

In C++, perfect forwarding is the act of passing a function’s parameters to another function while preserving its reference category. It is commonly used by wrapper methods that want to pass their parameters through to another function, often a constructor.

template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
    return std::unique_ptr<T>(
        new T(std::forward<Args>(args)...));
}

The make_unique function takes its parameters, forwards them to the T constructor, and then puts the pointer to the newly-constructed T inside a unique_ptr. Those parameters are forwarded perfectly into the constructor: If the original parameters were rvalue reference, then the constructor receives rvalue reference. If the original parameters were lvalue references, then the constructor receives lvalue reference.

But the catch is that it can forward only objects. It can’t forward “braced things that are trying to become objects”.

C++ exceptions and memory allocation failure -- Wu Yongwei

C++ exceptions are habitually disabled in many software projects. A related issue is that these projects also encourage the use of new (nothrow) for fear of exceptions. Is it good practice or bad practice? Also, how likely are we to really encounter out-of-memory errors? I will investigate on this topic and discuss strategies to deal with memory issues.

C++ Exceptions and Memory Allocation Failure

By Wu Yongwei

From the article:

In fact, every time we initialize or modify a string, vector, or map, we may be allocating memory on the heap. If we think that new will end in an exception (and therefore choose to use new (nothrow)), exceptions may also occur when using these mechanisms. In a program that has disabled exceptions, the result will inevitably be a program crash.

Ignoring abnormal scenarios like allocating more memory than the physical memory size at a time (which would likely be a logic error in the program), can a reasonable program still experience memory allocation failures?

Dynamic CUDA with NVIDIA's Runtime Compilation -- Richard Thomson

Utah C++ Programmers has released a new video:

Dynamic CUDA with NVIDIA's Runtime Compilation

by Richard Thomson

From the video description:

Using CUDA is great way to accelerate tasks on the GPU. Regular CUDA programming compiles your C++ code into code that can execute on the GPU. But what if your problem domain is more dynamic and requires generation of code on the fly?

On the CPU you can use techniques like LLVM to compile code Just-in-Time (JIT) into the native CPU instruction set and call this code directly from your application. What about on the GPU?

The NVRTC (Runtime Compilation) library accepts CUDA C++ source code and creates modules of GPU ready code that you can execute on the GPU.

This month, Richard Thomson will give us an introduction to using the NVRTC library for runtime compilation and nvJitLink for runtime linking of CUDA C++ source code to execute code on the GPU. We'll use a simple fractal generator as the example, with a user provided "iteration formula" to generate a fractal image in the complex plane.

https://www.youtube.com/watch?v=9OCQb5RA-s0