Skip to content

Instantly share code, notes, and snippets.

View Eisenwave's full-sized avatar

Jan Schultke Eisenwave

View GitHub Profile
@Eisenwave
Eisenwave / pointers.md
Last active October 23, 2024 20:43
How pointers in C++ actually work

How pointers in C++ actually work

Abstract: This document teaches you from the very basics to advanced features such as std::launder how pointers in C++ work. It is aimed at developers of any skill level. However, it links to the C++ standard so that advanced readers can verify the information and investigate further.

Motivation: Most tutorials on pointers are aimed at beginners, and present a simplified model. Some tutorials perpetuate an explanation of pointers that is solely based on their implementation (pointer = memory address). This tutorial aims to provide a comprehensive explanation of pointers that is in line with how they actually work from a language perspective.

@Eisenwave
Eisenwave / deducing_this_concisely.md
Last active October 19, 2023 16:23
Deducing this concisely

Deducing this concisely

P0847: Deducing this has introduced explicit object member functions. These greatly empower the developer, but come at a cost of added verbosity. This proposal offers two independent ways of reducing this verbosity.

Motivation

@Eisenwave
Eisenwave / byte_initialization.md
Last active September 13, 2023 22:20
C++26 Proposal Draft - Conversion of integer literals to std::byte

Conversion of integer-literals to std::byte

Abstract

This proposal aims to improve the ergonomics of the std::byte type by allowing the conversion of an integer-literal to std::byte. This would simplify initialization and comparison of std::byte, which would encourage its use, and make it feel more like a fundamental type.

Introduction

@Eisenwave
Eisenwave / case_against_almost_always_auto.md
Last active September 19, 2024 16:53
The case against Almost Always `auto` (AAA)

The case against Almost Always auto (AAA)

Introduction

I've been writing C++ for half a decade now, and auto has always been a great source of discomfort to me. Whenever I came back to a past project that makes extensive use of it, I found myself confused, and first had to look at all the types before I could make sense of it.

Similarly, I've worked as an intern at a company that had a AAA policy for its code base. Whenever something didn't work, and I had to debug some code, half the time was spent just looking up types.

@Eisenwave
Eisenwave / provable_if.md
Last active August 24, 2023 12:01
C++ proposal draft: Utilizing constant folding with provable if statements

Utilizing constant folding with provable if statements

A C++2b proposal.

Abstract

I propose a provable if statement which never incurs run-time cost, and utilizes either constant evaluation, or constant folding and constant propagation to determine whether its condition is true. Such a statement is useful for choosing alternative forms of algorithms if some constants are known, as well as improving diagnostics for failed assertions without any run-time cost.

@Eisenwave
Eisenwave / cpp-memory.md
Created August 2, 2023 16:39
The C++ Memory Model and Multithreading - According to the Standard

The C++ Memory Model and Multithreading
According to the Standard

All code you write is ultimately designed to read and write memory. One aspect of this is the memory model of the language; something which is accompanying us as developers, but we rarely have to think about.

How come, for example, the following code does what we expect it to?

int x = 1;
x = 5;
@Eisenwave
Eisenwave / cpp-keywords.md
Created July 20, 2023 11:46
The Effect of `inline`, `static`, etc. on Symbols in C++

The Effect of inline, static, etc. on Symbols in C++

inline

  • Free function: Makes it an inline function. Linkage is unchanged, and external by default.
  • Static member function: Makes it an inline function. Linkage is the same as that of the class. inline is redundant for inline definitions, except when modules are used. In that case, inline would make the function part of the module interface as usual.
  • Non-static member functions: Makes it an inline function. The same rules for linkag and modules apply.
@Eisenwave
Eisenwave / divison_proposal.md
Last active June 22, 2024 11:42
C++26 Proposal Draft - <intdiv> Header for Integer Divisions

<intdiv> Header for Integer Divisions

Introduction

C++ currently offers only truncating integer division. As a consequence, the remainder operator's sign is same as the sign of the dividend. Alternative rounding modes and remainder sign behaviour are useful.

This proposal adds an <intdiv> header containing free functions for obtaining the quotient and remainder for different rounding modes, such as rounding towards the nearest integer, towards the infinities, etc.

@Eisenwave
Eisenwave / divlu.c
Last active August 19, 2020 10:01
THIS IS A MIRROR OF http://www.hackersdelight.org/HDCode/divlu.c WHIS IS NO LONGER VIEWABLE
/* Long division, unsigned (64/32 ==> 32).
This procedure performs unsigned "long division" i.e., division of a
64-bit unsigned dividend by a 32-bit unsigned divisor, producing a
32-bit quotient. In the overflow cases (divide by 0, or quotient
exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible
value).
The dividend is u1 and u0, with u1 being the most significant word.
The divisor is parameter v. The value returned is the quotient.
Max line length is 57, to fit in hacker.book. */
* Long division, unsigned (64/32 ==> 32). This procedure performs unsigned "long division" i.e., division of a 64-bit unsigned dividend by a 32-bit unsigned divisor, producing a 32-bit quotient. In the overflow cases (divide by 0, or quotient exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible value). The dividend is u1 and u0, with u1 being the most significant word. The divisor is parameter v. The value returned is the quotient. Max line length is 57, to fit in hacker.book. */
#define max(x, y)((x) > (y) ? (x) : (y))
int nlz(unsigned x) {
int n;
if (x == 0) return (32);
n = 0;
if (x <= 0x0000FFFF) {
n = n + 16;
x = x << 16;