this post was submitted on 21 Mar 2024
20 points (100.0% liked)

Programming Horror

1891 readers
1 users here now

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there's also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @[email protected]

Rules

Credits

founded 1 year ago
MODERATORS
20
C++23 has fixed space! (kbin.melroy.org)
submitted 8 months ago* (last edited 8 months ago) by [email protected] to c/programming_horror
 
top 2 comments
sorted by: hot top controversial new old
[–] [email protected] 11 points 8 months ago* (last edited 8 months ago)

Just in case anyone asks (they always do):

I use Bionic Reader to bold the start of words so it reads faster.

I also use Toucan.

Oh, and just block that guy. I wonder if lemmy or mbin can implement IP-blocking.

[–] [email protected] 5 points 8 months ago* (last edited 8 months ago)

I forgot the alt text 🤦 here's the markdown (source):

Trimming whitespaces before line splicing

As of C++20, what's the output of the below piece of code (Godbolt)?

int main() {
int i = 1
// \ 
+ 42
;
return i;
}

Without having seen how the code is formatted I would have said that it's 1 + 42, so 43. And actually, that's the real output. On MSVC. Not on other compilers, such as GCC and Clang.

These latter ones return 1. The reason is that they trim the trailing whitespaces before handling \ splicing. The new-line character following \ is a whitespace, it's deleted, therefore + 42 ends up on the same line as // \ and it becomes part of the comment.

Just for the record, both implementation strategies are valid.

P2223R2 change this situation and set the GCC/Clang implementation strategy into ~~stone~~ standard. This might be a breaking one for code that is only compiled with MSVC, but it shouldn't happen on a big scale.

We might remember Matt Godbolt saying earlier in one of his talks that one of the superpowers of C++ is backward compatibility and here the change potentially breaks code. At the same time, it's worth noting that implementation-defined behaviour would break here and not standardized behaviour.

The reason behind going in this direction is that many IDEs, code formatters and other tools already discard trailing whitespaces as the Google style guide and most of its derivations already forbid them.

I've changed the Godbolt link and code because the original didn't have the trailing whitespace, lol.