C++ - Reddit
227 subscribers
48 photos
8 videos
1 file
25.6K links
Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.

Join our group for discussions : @programminginc

Powered by : @r_channels
Download Telegram
Catastrophic misoptimization of byte-range equality comparison in all major C++ compilers (and Rust). Language issue or compiler issue?

Someone brought a C++ snippet that reduced to the following (std::array used instead of char* for consistency for later)

bool eq2(const std::array<char, 64>& lhs, const std::array<char, 64>& rhs) {
for (int i = 0; i < 64; ++i)
if (lhs[i] != rhs[i])
return false;
return true;
}

It basically returns true iff two 64 byte memory regions are a bitwise match. You'd think this can be optimized by modern compilers flawlessly, right? Right?! And the worst thing that can happen is it doesn't get autovectorized, right? Right?!

Well, to everybody's surprise... https://godbolt.org/z/v4d8G5ovh

GCC attempts to partially conditionally vectorize but doesn't want to use unaligned loads. Moreover its vectorization is way more cumbersome than it needs to be. Then after it finds a mismatch it... reverts to the unvectorized version?! Clang and MSVC do not even attempt to vectorize it - and in a way it makes them end up better than GCC, which is wild.

----------------

Now, to try to investigate we've checked how it looks in Rust. https://godbolt.org/z/K7xE3qc94. It does what GCC does but fully vectorized.

At this point we're confused as to what the source of the problem is. Are we missing something with respect to what transformations are allowed? Is it really that much to ask from the as-if rule?

As a sanity check we use std::array's operator==. https://godbolt.org/z/xPhPYMjvz. Oh would you look at that? Now the compilers can do well (ok not you MSVC. GCC also fumbles the flag register slightly). The situation is the same in Rust - https://godbolt.org/z/3GjWorze8. Not only does it vectorize correctly but the vectorized code is better - using vpxor.

-----------------

Ultimately this isn't just academical. Situations like this undermine the trust in the compiler at the most fundamental level. Not every place can use std::array, not everyone can use ranges and span yet. We could use `std::equal` but why does it matter so much? What if `!=` changes to `<` and there's no good solution anymore? To what degree are we supposed to be paranoid about basic optimizations being done or not?

The similarity of codegen between Rust and C++ would point to it being either a compiler issue or a fundamental code correctness issue. The fact that C++ Clang differs from Rust (LLVM) would suggest it's at least partially a language issue. Why is this extremely simple code so nasty?

https://redd.it/1xlyti8
@r_cpp
New C++ Conference Videos Released This Month - November 2026

**C++Day**

2026-10-26 - 2026-11-01

* \[C++ Day 2026\] Old-School C++ for the Agentic Age: Lessons from Sane C++ (Stefano Cristiano) - [https://www.youtube.com/watch?v=tAf-HsET0pE](https://www.youtube.com/watch?v=tAf-HsET0pE)
* \[C++ Day 2026\] Zero-cost GIS: parsing binary formats in C++23 (Giorgio Bignotti) - [https://www.youtube.com/watch?v=VHeNHf6t76Q](https://www.youtube.com/watch?v=VHeNHf6t76Q)

**C++Now**

2026-10-26 - 2026-11-01

* AI is Forcing us to Obey Scott Meyers - Jody Hagins - [https://youtu.be/CiaZq4kCH08](https://youtu.be/CiaZq4kCH08)
* C++ Libraries - The Road Ahead - Jeffrey Garland - [https://youtu.be/bNipVnNaDdk](https://youtu.be/bNipVnNaDdk)
* Lightning Talk: ?: - Jonathan Müller - [https://youtu.be/TLj6NVx5xvk](https://youtu.be/TLj6NVx5xvk)

**ACCU**

2026-10-26 - 2026-11-01

* Creating Composable Callables in Contemporary C++ - Using Senders / Receivers as a Frame Graph - Björn Fahller - [https://youtu.be/A4mMO5xeQDA](https://youtu.be/A4mMO5xeQDA)
* Incrementally Migrating C++ to a Memory Safe Language with Swift - Tim Condon - [https://youtu.be/LMQ21X6\_2FQ](https://youtu.be/LMQ21X6_2FQ)
* CallMeMaybe - Building Modern Runtime Reflection via C++26 - Laurie Kirk - [https://youtu.be/cA0F3wRtZIU](https://youtu.be/cA0F3wRtZIU)

**ADC**

2026-10-26 - 2026-11-01

* TX/RX Part 2 - AI Audio on the Raspberry Pi - Kieran Coulter - [https://youtu.be/QSiyLDHoBpo](https://youtu.be/QSiyLDHoBpo)
* JUCE in DJ Products - Current Status and Future Prospects - Masamichi Tomii, Akihito Yamada & Tetsuro Abe - [https://youtu.be/VElORkcpE78](https://youtu.be/VElORkcpE78)
* Generative Instruments with Large Piano Models - Tomek Roszczynialski - [https://youtu.be/EO-hGj8\_BUY](https://youtu.be/EO-hGj8_BUY)

https://redd.it/1xm10uv
@r_cpp
Pretty nasty MSVC 19.51 hash of float bug

Hash of 0.0 and -0.0 are not the same, as the condition:

_Keyval == 0.0F ? 0.0F : _Keyval


is optimized away if you compile with:

/arch:AVX2 /O2


https://godbolt.org/z/45WPzceYx

If you use Visual Studio 2022 it works fine. I'm somewhat surprised they don't have regression tests since the standard library explicitly handles this case.

So I guess, check you hash maps if you update.

https://redd.it/1xmr0xz
@r_cpp
C v.s. C++

Is it worth it to learn both? Preferences... main differences? Starting to learn C++ & someone suggested that I learn C in addition. I suppose I don't see the point since they're similar. When would it matter - or in what cases would I need to know C in addition to C++ ??

https://redd.it/1xnlc40
@r_cpp
Is loop unrolling one of the speed booster

Long story short, I tried to check performance of C++ vs python by checking if file exists.

Here after - naive loop unrolling, seems the C++ code speed boosted. Please suggest other hacks/suggestions to make C++ run faster. Also if any books, links, github - please. I request C++ professionals


import os.path

FILES_DIRECTORY : str = "data"

def missing_parser(_skip_index: int, _max_limit : int = 400):
_cur_index : int = _skip_index
_max_limit = _skip_index + 400
while _cur_index < _max_limit:
for j in range(_cur_index, _cur_index+100):
if os.path.exists(os.path.join(FILES_DIRECTORY, str(j)+".pkl")):
print("Path exists",str(j)+".pkl")
else:
print("Missing ",str(j)+".pkl")
_cur_index += 100

missing_parser(1)


## Python time taken

real    0m0.750s
user 0m0.011s
sys 0m0.108s


#include<iostream>
#include<fstream>
#include<string>
#include<filesystem>

#define FILE_DIRECTORY = "data"

void check_files(unsigned _start, unsigned _end){

std::string file_name;

for(unsigned k=_start; k<=_end; k++){
bool file_exists = false;
std::string filename = "./data/"+std::to_string(k)+".pkl";

file_exists = std::filesystem::exists(filename);

if(file_exists){
std::cout<<"File Exists\n";
}else{
std::cout<<"File not exists\n";
}
}
}

int main(int argc, char const *argv[]){


check_files(1, 400);

return 0;
}


C++ default build, time taken

real    0m0.706s
user 0m0.004s
sys 0m0.080s


Default compilation was slower than python, but after adding "Release" flag on, the CMake generated build, faster by 0.10 seconds.

Now the final part

#include<iostream>
#include<fstream>
#include<string>
#include<filesystem>

#define FILE_DIRECTORY = "data"

void check_files(unsigned _start, unsigned _end){

std::string file_name;

for(unsigned k=_start; k<=(_end/10)+1; k++){
bool file_exists[10] = {0};

std::string filename1 = "./data/"+std::to_string(k)+".pkl";
std::string filename2 = "./data/"+std::to_string(k+1)+".pkl";
std::string filename3 = "./data/"+std::to_string(k+2)+".pkl";
std::string filename4 = "./data/"+std::to_string(k+3)+".pkl";
std::string filename5 = "./data/"+std::to_string(k+4)+".pkl";
std::string filename6 = "./data/"+std::to_string(k+5)+".pkl";
std::string filename7 = "./data/"+std::to_string(k+6)+".pkl";
std::string filename8 = "./data/"+std::to_string(k+7)+".pkl";
std::string filename9 = "./data/"+std::to_string(k+8)+".pkl";
std::string filename10 = "./data/"+std::to_string(k+9)+".pkl";

file_exists[k] = std::filesystem::exists(filename1);
file_exists[k+1] = std::filesystem::exists(filename2);
file_exists[k+2] = std::filesystem::exists(filename3);
file_exists[k+3] = std::filesystem::exists(filename4);
file_exists[k+4] = std::filesystem::exists(filename5);
file_exists[k+5] = std::filesystem::exists(filename6);
file_exists[k+6] = std::filesystem::exists(filename7);
file_exists[k+7] = std::filesystem::exists(filename8);
file_exists[k+8] = std::filesystem::exists(filename9);
file_exists[k+9] = std::filesystem::exists(filename10);

if(file_exists[k]){
std::cout<<k<<"File Exists\n";
}else{
std::cout<<"File not exists\n";
}

if(file_exists[k+1]){
std::cout<<k<<"File Exists\n";
}else{
std::cout<<"File not exists\n";
}

if(file_exists[k+2]){
std::cout<<k<<"File Exists\n";
}else{
std::cout<<"File not exists\n";
}

if(file_exists[k+3]){
std::cout<<k<<"File Exists\n";
}else{
std::cout<<"File not exists\n";
}

//so on, removed due to code length

}
}

int main(int argc, char const *argv[]){

check_files(1, 400);

return 0;
}


The final version is faster by 0.2 second.

when measured by $ time command,

real    0m0.512s
user 0m0.000s
sys 0m0.055s


https://redd.it/1xnoq3m
@r_cpp
duolingo-like application?

I was wondering if someone has a nice duolingo-like application for practicing , and not losing problem solving skills

https://redd.it/1xns0bv
@r_cpp
Maybe C++ would win if it adapted quicker?

I know that I'm oversimplifying here. The way the new C++ standard is released is pushing the language behind the others, especially those who adapt new language features quicker and favorize new modern language features.

I'm not saying that the movement towards modern C++ (whatever that might mean) is not helping. I love the fact that there are new exciting features such as reflection in the C++26 standard. Saying that however, even though the standard is supposed to have these features, the adaptibility by the compilers and the ecosystem is extremely slow.

Here we are in 2026 and MSVC as of now has zero C++26 core language features implemented. And god knows how long they will be unavailable. I feel like only now when its 2026, c++23 started to be slowly adopting, which I find sad. PS: I'm aware that GCC has mostly all features implemented, but STILL not all of them. Similar situation for Clang.

Maybe a more "aggressive" and faster release cycle would help? Also faster deprecation and language cleanup? The general push has always been towards backwards compatibility, but you can't always add new things that people want without breaking compatibility and removing deprecated features. And I think that for the language it's very important to adapt the modern programming language standards. But now I don't feel like that's happening.

Obviously there are other issues with C++, such as not having standardized package managers, linters, formatters, etc. For example, the clang-tidy formatter is abysmaly slow, and just painful to use, probably because it has to accomodate the million features the C++ language permits, and even the things that doesn't make sense no more and are just left in the language as obsolete things, ready to be removed after 20 years, which is hilarious.

Anyway, what are your thoughts on this topic?


EDIT: I agree that chosing the wording "win" is a bad wording here. The main point was to address some of the weaknesses C++ has compared to other languages/ecosystems.

https://redd.it/1xnx9k2
@r_cpp
[C++17] easyXshm – A lightweight X11 MIT-SHM framebuffer renderer

I've been working on easyXshm, a small C++17 library that makes framebuffer-based rendering through X11's MIT-SHM extension easier to work with.

The motivation was pretty straightforward: I wanted a simple shared-memory rendering API without having to deal with X11 internals throughout my application.

The project focuses on:

* **Simple API:** Get a basic rendering workflow running in approximately 5 minutes.
* **Framebuffer-first:** Work directly with pixel data instead of dealing with a complicated graphics abstraction.
* **Input handling:** Keyboard and mouse events through straightforward inline helper functions.
* **Minimal overhead:** Uses X11 MIT-SHM for shared-memory image transfer.
* **Small scope:** Rendering and input handling, without trying to become a complete graphics framework.

The goal is to make the difficult parts stay inside the library while keeping the application-side code readable.

I'm currently looking for technical feedback, particularly regarding API design, memory management, portability and edge cases.

Repository: [https://github.com/intriXlabs/easyXshm](https://github.com/intriXlabs/easyXshm)

**AI disclosure:** This Reddit post was written with assistance from AI. The project implementation and engineering work are mine.

I'd appreciate feedback from people familiar with C++, X11 or Linux graphics programming.

https://redd.it/1xo084q
@r_cpp
C++ Show and Tell - November 2026

Use this thread to share anything you've written in C++. This includes:

* a tool you've written
* a game you've been working on
* your first non-trivial C++ program

The rules of this thread are very straight forward:

* The project must involve C++ in some way.
* It must be something you (alone or with others) have done.
* Please share a link, if applicable.
* Please post images, if applicable.

If you're working on a C++ library, you can also share new releases or major updates in a dedicated post as before. The line we're drawing is between "written in C++" and "useful for C++ programmers specifically". If you're writing a C++ library or tool for C++ developers, that's something C++ programmers can use and is on-topic for a main submission. It's different if you're just using C++ to implement a generic program that isn't specifically about C++: you're free to share it here, but it wouldn't quite fit as a standalone post.

Last month's thread: https://www.reddit.com/r/cpp/comments/1wwiz8g/c_show_and_tell_october_2026/

https://redd.it/1xo0gru
@r_cpp
Anyone working on a C++ project who needs someone with testing?

Hey guys!

I’m looking to get involved in a C++ project where I can contribute with testing.

I have 3 years of experience as a Test Software Engineer, with a background in manual testing, test automation, test case design and debugging.

If anyone is currently working on a C++ project and could use an extra pair of hands for testing, I’d be happy to contribute !

Open-source projects or personal projects are both welcome.

Cheers!

https://redd.it/1xo36ya
@r_cpp
On the scale of 1-10, am I cooked?

I took "How solid are your C++ fundamentals?", a quiz written by Mateusz Pusz, author of mp-units. I scored 42%, which put me higher than 54% of the 349 people who had taken it so far.

Some context:

🔹 I'm a 3rd-semester CS student at Abdul Wali Khan University, Mardan.

🔹 I have no internship, no job, and zero professional C++ experience.

🔹 The topics this quiz covers (the ODR, value categories, exception-safety guarantees, argument-dependent lookup) were never part of my coursework. I learned C++ on my own over 3 years of grinding, with no mentor and no one guiding me.

🔹 For the last 1.5 years I haven't written much meaningful C++ beyond uni lab sessions. I've been focused on Linux and building my own workflow and desktop environment around Hyprland.

My breakdown:

• Objects in memory: 58%

• The type system: 52%

• Functions and safety: 30%

• Names and language plumbing: 27%

Give it a try: https://train-it.eu/quiz/how-solid-are-your-cpp-fundamentals/

https://redd.it/1xo4ebp
@r_cpp
New naming convention for private variables

Good evening. Have come back to some C++ projects and I'd like to propose a new naming convention for private variables.

Commonly seen, as far as I know, is prefixed; _ | m | m_ , or suffixed _.

Today I started with mr (mister), and now I have a mrSceneManager, mrCam and mrViewport.
It's really fun and now I see them as characters appearing managing things.

Yeay or Nay?

https://redd.it/1xosmes
@r_cpp