C++ Prelude
Modern C++ makes me sad when I look at it. So this is my attempt to change several of the defaults we use to code in C++. Additionally, several features that are missing in the language (but enabled through obscure compiler macros and directives) are enabled.
project page
Includes Several containers and algorithms can be included using the or or directive. I use these very frequently, others might not. For that reason they are opt-in. To enable them define the the containers or algorithms directive before including the header.
Like this:
The following containers are included:
- unordered_map
- unordered_set
- queue
- stack
- list
- span
The following algorithms are included:
- algorithm
- ranges
- numeric
The following io headers are included:
- format
- fstream
The following headers are included by default:
- cstdint
- limits.h
- iostream
Types A mix of the naming scheme in Rust and HolyC is very aesthetically pleasing. Types become terse with only the information you need at a first glance. Furthermore, for consistency, all types should begin with capital letters.
Numeric Types
Floats Most of the floating types that are mentioned in IEEE 754.
| Prelude | Vanilla |
|---|---|
| N/A | |
| N/A | |
| N/A | |
| N/A | |
| N/A or | |
| N/A | |
| N/A |
Additional information on the brain floating data type.
| Prelude | Vanilla |
|---|---|
| N/A | |
| N/A | |
| N/A |
These changes beg the question: What is wrong with ? Well would be a perfectly valid type if C++ supported Big Integers like Java does. The problem is is fixed to a type that is sometimes 32-bits and other times something else. I think this is very inelegant and needlessly terse. It is better to simply know what kind of integer and bit-width it takes up.
The only exception to this rule being and . For the former we want to preserve the ability to scale depending on what system runs the binary. For the latter we want to optimize the CI development cycle, so the less worries the better. If you want an then have an .
Other Types
| Prelude | Vanilla |
|---|---|
| N/A | |
In most situations, it is clearer to write the type as a than a . Same goes for . This also remains consistent with the implementation of smart pointers in the standard library, for example .
are enabled by default.
Keywords
| Prelude | Vanilla |
|---|---|
You define terms as . If you need the term to be of any specific numeric type then use suffixes like: (x is unsigned). If you need a variable term then use the keyword instead. The motivation for this is that you should prefer immutable terms before considering mutable variables.
All functions should be and be annotated with trailing return types. If you need to ignore the return value of a function then use to explicitly ignore it. Additionally, the term (function) should be used in its most literal mathematical sense, for a given input it will always produce the same output. If you want to do something that requires side effects then use the (procedure) keyword instead.
, and naming conventions are taken from Haskell. The motivation being that is extremely verbose for accessing the first/second/third/etc members of that tuple.
Better Main The way is defined for use is very old. It is not type safe and uses old types like c-strings. We can, instead, make a better that is the one we define our program in and forward the arguments in a type-safe manner.
Implementation taken from C++ Weekly ep 361 by Jason Turner
NOTE: It has been mentioned that has historically been used for passing sensitive information like passwords to programs. In that case, the array used to store the different arguments is chopped up to remove the sensitive information. For now, this method does not allow do that. Furthermore, sending sensitive information through command line arguments is a bad idea IMO.
Short example Solution for Rosetta Code "Balanced Brackets" using this prelude.
TODO:
- Add algorithms
- Improve range algorithms
Discussion in the ATmosphere