I'm currently optimizing lexy's JSON parser example.
I've now reached a point where it is 12000% faster than all other JSON libraries.
The trick is to consider all characters whitespace and skip them...
To countless people in the reddit comment section: No, somebody implementing their own language after they've failed to change an existing one, isn't throwing a tantrum.
Programming languages are tools. If they don't work for you, switch. If no alternative exists, invent one.
New blog post: I wrote a compile-time Brainfuck compiler that generates optimized assembly.
The best part: I didn't need to do anything, I've tricked clang into doing it for me.
Technique: Compile Time Code Generation and Optimization
#cpp
#cplusplus
"We have strlen() to get the length of a string, strcpy() to copy a string, and strcmp() to compare a string.
How should we call the function to search inside a string?"
"Eh, I don't know. str..., str..."
"Perfect, thanks!"
To improve my personal development experience I will at some point in my life write
1. My own programming language.
2. My own text editor/IDE.
3. My own OS and/or window manager.
The question is: in what order?
Shoutout to MSVC for ignoring [[no_unique_address]] and forcing you to use [[msvc::no_unique_address]] instead.
That's exactly the point of standardized attributes.
I have just released the first non-beta version of lexy.
Want to parse anything in C++? Try it out. It has Unicode support, automatic error recovery, and many more features.
#cpp
#cplusplus
@vzverovich
1. Strict aliasing violations.
2. Union type punning,.
3. std:vector
4. mmap
5. arbitrary pointer subtraction
6. treating raw memory as objects
7. signed integer overflow
8. function pointer casts
And that's just my current project!
I've spent the last half hour looking for the bug in my coroutine related code for my upcoming
@ACCUConf
talk.
Turns out, it was this for loop:
for (auto i = 0; i != steps; ++steps)
std::deque seems to be an underrated container:
* you can efficiently insert at the front
* the elements have pointer stability
* reallocation doesn't require copying things around
Sure, pure iteration is slower as it's a collection of segmented array - am I missing sth else?
The lifecycle of a C++ library developer:
1. You implement a feature.
2. You work around bugs in older version of GCC and clang.
3. You work around bugs in the current version of MSVC.
4. You work around MSVC some more.
5. Eh, maybe this works?
6. No?
7. ???
8. Profit.
I've just tagged the first (beta) release of lexy, my C++ parser DSL library!
It features parse tree generation, automatic error recovery, operator precedence parsing, Unicode support, and much more.
#cpp
#cplusplus
I've asked for ChatGPT's opinion on
@jfbastien
's "zero-initialize objects of automatic storage duration" proposal.
Are we sure it hasn't been trained on the committee mailing lists?!
The C++20 range based algorithms with projections are so much more convenient:
std::find_if(vec.begin(), vec.end(), [&](T& elem) { return elem.member == needle; }
vs
std::ranges::find(vec, needle, &T::member);
I'd like to use this opportunity to remind you about my website (for no particular reason):
There you'll find a list of my talks and projects, as well as links to e.g. my GitHub account.
Every global variable should be
0. non-existent, or
1. constexpr, which means it's immutable and initialized at compile time, or
2. constinit, which means it's mutable and initialized at compile time.
That way, there is no static initialization order fiasco.
#cpp
#cplusplus
C++ standard library in a nutshell:
1. Standardize a cool algorithm/container.
2. Early implementation is rushed and/or makes a decision that did not age well.
3. Early implementation cannot be changed due to ABI making it unusable.
4. Repeat with the new alternative.
Pro tip if you want to expose a lot of bugs: change your custom allocator implementation so that it immediately returns memory to the OS instead of caching it.
Quick way to get use-after-free errors.
I recently needed to iterate over a null-terminated string via iterators, but I didn't want to calculate the end iterator up front. Luckily, this is easily done with a sentinel.
Tutorial: C++20's Iterator Sentinels
#cpp
#cplusplus
I've been reading "C++ has become a legacy language" a lot recently; I'm curious what you think.
Has or will C++ become a legacy language in the near future?
Please RT for reach.
#cpp
#cplusplus
#rust
#safety
TIL: division by -1 on x86 can also raise a division by zero error, which is a bit confusing to look at in the debugger.
I also did not want to learn that.
@beyerstein
It's illegal in Germany. If you can create a contract online by clicking a button, you must be able to cancel it online by clicking a button; cancellation must not be more difficult than subscribing.
Just wrote twenty slack messages explaining the difference between `T t;`, `T t{};`, `T()` and `T{}` (basically**** the first doesn't do zero-initialization, the others might)
C++ truly is a language where initialization is a discussion among experts, for experts.
I published a new project I've been working on for the past two months or so:
foonathan/array - an array based container library
Featuring: std::vector equivalent with powerful customization for SBO & co, flat sets/maps etc
#cpp
#cplusplus
My malloc blog post results in two reactions:
If you're coming from a C++ perspective, you see malloc as a low-level API that you only use for absolute performance. Then you agree.
If you're coming from C, malloc is your convenient allocator and any change a complication.
New blog post about naming with a small detour about the C++ object model, because why not.
Naming Things: Implementer vs. User Names
Thanks to
@gregcons
@CppCon
talk about naming for giving me the proper term for the symptom!
#cplusplus
#cpp
I've surpassed 10k followers!
When I created this twitter account almost exactly 8 years ago, I had no idea how much it would change my life: it caused me to do open source projects, start a blog, give talks, and I got to meet so so many amazing people.
Thank you all.
A while back, I've started to write comments as full sentences, with punctuations and everything.
I've noticed that I am consistently using "we" in the comments.
// Check, whether we've reached the end.
// We need to handle -1 as well.
etc.
I sort of like it.
The nice thing about writing a library that is almost completely templates: you have really high code coverage by default.
Functions are only measured if you have a test that instantiates them. If you want 100% code coverage, just delete all tests!
I've created a couple of docker images for building C++ projects. They're very lightweight and are designed to create fully statically linked executables compatible with any Linux distribution.
#cpp
#cplusplus
#docker
I wrote a short blog post that might be interesting for the C++ bloggers out there:
Tutorial: Interactive code snippets with Hugo and
@CompileExplore
#cpp
#cplusplus
For me personally, Rust has two advantages over C++:
1. A modern, clean, usable standard library.
2. Only having to support a single compiler frontend.
Now that GCC will gain a Rust frontend, the second advantage is gone.
Pro tip for custom compile-time error messages:
template <typename ... Args>
constexpr bool error_with = false;
Then:
static_assert(error_with<A, B, C>, "A not compatible with B and C");
clang will show:
static_assert failed ... 'error_with<int, char, float>' "A not ..."
The nice thing about C++ is that you can be the first person to use a certain combination of features!
Today, unnamed bit fields and has_unique_object_representations. Bonus points for broken operator== on clang.
#cpp
#cplusplus
@lefticus
It depends.
Here it is singular, as `Color c` will only ever hold one color.
But if your enum is used as flag, one enum variable might hold a combination of flags, so it is plural.
I'm releasing a new library tomorrow!
I've been working on it for the past year or so, going through various designs, trying things out, and now I've finally got something ready to be released.
If anyone wants to proofread the tutorial I've just written, let me know.
There is a big discussion about Linux and Rust's safety, but most seems to focus on an earlier email by Linus. I find this reply more interesting:
I think those functions should be unsafe as they can lead to UB, but Linus doesn't seem to care much
#rust
C++ is a low-level language: instead of the language features you actually want, you have tools to build them.
Want customization functions? Build something using friend functions, arcane look up rules and overloading.
Want to tweak move behavior? Use this weird reference type.
Tools ranked by how long it takes to fix their error messages, ascending:
0. clang-tidy
1. C compiler
2. C++ compiler (non-template)
3. CMake
4. C++ compiler (template)
...
999. Linker
I wrote my thoughts about writing a new type vs adding new functions to change behavior:
saturating_add vs. saturating_int – new function vs. new type?
#cpp
#cplusplus
The problems of an open source maintainer:
1. Write code.
2. Test code.
3. Document code.
4. Workaround MSVC.
5. Crash GCC in the process.
6. Spend an afternoon running creduce.
7. ??? (I'm not here yet)
8. Release.
Only 1. and 8. is actually fun...
Carbon has a feature that I've been thinking about for years, and which has a lot of potential, so I've blogged about it:
Carbon’s most exciting feature is its calling convention
#carbon
#CarbonLang
Functions should be small.
What if you have a big function that does three things in sequence though?
If you extract it into three functions step1(); step2(); step3(), this looses information about the potential order.
Ping
@tvaneerd
In some compiler messages, you are reminded that C++ doesn't support default-int.
While I'm sure this was helpful to include 40 years ago, I'd argue that most people are getting confused by it since they have no idea what default int is.
#cpp
Days since the last compilation error because I forgot a constexpr on a function: 0
Remember, if you don't have a test that calls your constexpr function at compile-time, your function isn't really constexpr!
Writing documentation is great way to discover bugs and interface inconsistencies.
It is also great to go "ah, I should probably do that here" only to discover that you already thought of that, and it's been implemented for ages...
Hey, I have decided to setup a Patreon. If you like my work, I'd love to get your support.
Thank you all for blog post comments, Github stars, issues and PRs and way too many followers!
Immediately-Invoked Function Expression:
struct iife {};
template <typename Func>
auto operator+(iife, const Func& f) { return f(); }
#define
IIFE(T) iife{} + [&]() -> T
// x can be const, even though...
const auto x = IIFE(int) {
... // complex init here
};
cc
@gregcons
In the C++ UG meeting in Aachen last month a string switch based on tries was presented. This inspired me to write a tokenizer based on the same mechanism.
The result is a very fast C++14 (compile-time) tokenizer:
#cpp
#cplusplus
Exams are over, time for a new blog post: This is the C++ heaviest post of the series where I talk about <=>
Mathematics behind Comparison
#4
: Three-Way Comparison
#cpp
#cplusplus
I may have created yet another library...
foonathan/tiny: low-level library featuring tombstones, improved bitfields (tiny types) and other tricks to make your types as small as possible.
#cpp
#cplusplus
I'd normally wouldn't quote tweet to disagree but since this tweet blew up, I feel I need to for visibility.
What he's saying here is false and misleading. Circle isn't being censored from the subreddit, the occupation of the mod is irrelevant, and only a single post was removed
Circle is being censored from the C++ reddit. I built a C++ compiler from scratch. I'm documenting my work on extensions that are aimed at keeping it from going extinct. But a mod who works for a commercial vendor is now pulling down posts. This is terrible for free discourse.
Every C++ class should be exactly one of:
* abstract (base class in hierarchy)
* polymorphic *and* final (derived class in hierarchy)
* non-polymorphic, public dtor (regular class)
* non-polymorphic, protected dtor (non-polymorphic base classes)
#cpp
#cplusplus