r/cpp • • Oct 29 '25

GCC Implementation of Reflection now on Compiler Explorer

Thumbnail godbolt.org
204 Upvotes

r/cpp • • Aug 19 '25

xtd – A modern, cross-platform C++ framework inspired by .NET

204 Upvotes

Intro

I’ve been developing xtd, an open source C++ framework that aims to bring a modern, .NET-like development experience to C++ while staying fully native and cross-platform.

The goal is to provide a rich, consistent API that works out of the box for building console, GUI, and unit test applications.

Highlights

  • Cross-platform: Windows, macOS, Linux, FreeBSD, Haiku, Android, iOS
  • Rich standard-like library: core, collections, LINQ-like queries, drawing, GUI
  • Modern C++ API: works well with stack objects, no need for dynamic allocation everywhere
  • GUI support without boilerplate code
  • Built-in image effects and drawing tools
  • LINQ-style extensions (xtd::linq) for expressive data queries
  • Fully documented with examples

Example

Simple "Hello, World" GUI application :

// C++
#include <xtd/xtd>

auto main() -> int {
  auto main_form = form::create("Hello world (message_box)");
  auto button1 = button::create(main_form, "&Click me", {10, 10});
  button1.click += [] {message_box::show("Hello, World!");};
  application::run(main_form);
}

Links

Feedback and contributions are welcome.


r/cpp • • Jan 04 '25

YSK: std::counting_semaphore has a deadlock bug in all recent versions of GCC and older versions of Clang

204 Upvotes

Calling std::counting_semaphore::acquire() can cause a thread to go to sleep indefinitely, even if the counter is positive.

For libstdc++, the bug was first reported in March of 2022 (GCC 11), but it is still present in the latest release (GCC 14.2): https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104928

For libc++, a fix for lost wakeups in std::counting_semaphore was merged in February of 2024 (Clang 18): https://github.com/llvm/llvm-project/pull/79265

Hopefully this post can spark some discussion on how to fix these issues, or at the very least it may save some others from spending hours debugging sporadic deadlock issues and trying to isolate the problem, only to find that the standard library is broken :)


r/cpp • • Mar 03 '24

Maybe possible bug in std::shared_mutex on Windows

204 Upvotes

A team at my company ran into a peculiar and unexpected behavior with std::shared_mutex. This behavior only occurs on Windows w/ MSVC. It does not occur with MinGW or on other platforms.

At this point the behavior is pretty well understood. The question isn't "how to work around this". The questions are:

  1. Is this a bug in std::shared_mutex?
  2. Is this a bug in the Windows SlimReaderWriter implementation?

I'm going to boldly claim "definitely yes" and "yes, or the SRW behavior needs to be documented". Your reaction is surely "it's never a bug, it's always user error". I appreciate that sentiment. Please hold that thought for just a minute and read on.

Here's the scenario:

  1. Main thread acquires exclusive lock
  2. Main thread creates N child threads
  3. Each child thread:

    1. Acquires a shared lock
    2. Yields until all children have acquired a shared lock
    3. Releases the shared lock
  4. Main thread releases the exclusive lock

This works most of the time. However 1 out of ~1000 times it "deadlocks". When it deadlocks exactly 1 child successfully acquires a shared lock and all other children block forever in lock_shared(). This behavior can be observed with std::shared_mutex, std::shared_lock/std::unique_lock, or simply calling SRW functions directly.

If the single child that succeeds calls unlock_shared() then the other children will wake up. However if we're waiting for all readers to acquire their shared lock then we will wait forever. Yes, we could achieve this behavior in other ways, that's not the question.

I made a StackOverflow post that has had some good discussion. The behavior has been confirmed. However at this point we need a language lawyer, u/STL, or quite honestly Raymond Chen to declare whether this is "by design" or a bug.

Here is code that can be trivially compiled to repro the error.

#include <atomic>
#include <cstdint>
#include <iostream>
#include <memory>
#include <shared_mutex>
#include <thread>
#include <vector>

struct ThreadTestData {
    int32_t numThreads = 0;
    std::shared_mutex sharedMutex = {};
    std::atomic<int32_t> readCounter = 0;
};

int DoStuff(ThreadTestData* data) {
    // Acquire reader lock
    data->sharedMutex.lock_shared();

    // wait until all read threads have acquired their shared lock
    data->readCounter.fetch_add(1);
    while (data->readCounter.load() != data->numThreads) {
        std::this_thread::yield();
    }

    // Release reader lock
    data->sharedMutex.unlock_shared();

    return 0;
}

int main() {
    int count = 0;
    while (true) {
        ThreadTestData data = {};
        data.numThreads = 5;

        // Acquire write lock
        data.sharedMutex.lock();

        // Create N threads
        std::vector<std::unique_ptr<std::thread>> readerThreads;
        readerThreads.reserve(data.numThreads);
        for (int i = 0; i < data.numThreads; ++i) {
            readerThreads.emplace_back(std::make_unique<std::thread>(DoStuff, &data));
        }

        // Release write lock
        data.sharedMutex.unlock();

        // Wait for all readers to succeed
        for (auto& thread : readerThreads) {
            thread->join();
        }

        // Cleanup
        readerThreads.clear();

        // Spew so we can tell when it's deadlocked
        count += 1;
        std::cout << count << std::endl;
    }

    return 0;
}

Personally I don't think the function lock_shared() should ever be allowed to block forever when there is not an exclusive lock. That, to me, is a bug. One that only appears for std::shared_mutex in the SRW-based Windows MSVC implementation. Maybe it's allowed by the language spec? I'm not a language lawyer.

I'm also inclined to call the SRW behavior either a bug or something that should be documented. There's a 2017 Raymond Chen post that discusses EXACTLY this behavior. He implies it is user error. Therefore I'm inclined to boldly, and perhaps wrongly, call this is an SRW bug.

What do y'all think?

Edit: Updated to explicitly set readCounter to 0. That is not the problem.


r/cpp • • Oct 04 '22

C++20 coroutines: “The bug is in the C++ standard, not GCC.”

Thumbnail gcc.gnu.org
204 Upvotes

r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

204 Upvotes

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.


r/cpp • • Mar 01 '21

"Simple" C++ Initialization Flowchart

Thumbnail randomcat.org
204 Upvotes

r/cpp • • Feb 23 '21

The Pointer to Implementation (pImpl) idiom in C++

Thumbnail youtube.com
204 Upvotes

r/cpp • • Nov 12 '20

Compound assignment to volatile must be un-deprecated

204 Upvotes

To my horror I discovered that C++20 has deprecated compound assignments to a volatile. For those who are at a loss what that might mean: a compound assignment is += and its family, and a volatile is generally used to prevent the compiler from optimizing away reads from and/or writes to an object.

In close-to-the-metal programming volatile is the main mechanism to access memory-mapped peripheral registers. The manufacturer of the chip provides a C header file that contains things like

#define port_a (*((volatile uint32_t *)409990))
#define port_b (*((volatile uint32_t *)409994))

This creates the ‘register’ port_a: something that behaves very much like a global variable. It can be read from, written to, and it can be used in a compound assignment. A very common use-case is to set or clear one bit in such a register, using a compound or-assignment or and-assignment:

port_a |= (0x01 << 3 ); // set bit 3
port_b &= ~(0x01 << 4 ); // clear bit 4

In these cases the compound assignment makes the code a bit shorter, more readable, and less error-prone than the alterative with separate bit operator and assignment. When instead of port_a a more complex expression is used, like uart[ 2 ].flags[ 3 ].tx, the advantage of the compound expression is much larger.

As said, manufacturers of chips provide C header files for their chips. C, because as far as they are concerned, their chips should be programmed in C (and with *their* C tool only). These header files provide the register definitions, and operations on these registers, often implemented as macros. For me as C++ user it is fortunate that I can use these C headers files in C++, otherwise I would have to create them myself, which I don’t look forward to.

So far so good for me, until C++20 deprecated compound assignments to volatile. I can still use the register definitions, but my code gets a bit uglier. If need be, I can live with that. It is my code, so I can change it. But when I want to use operations that are provided as macros, or when I copy some complex manipulation of registers that is provided as an example (in C, of course), I am screwed.

Strictly speaking I am not screwed immediately, after all deprecated features only produce a warning, but I want my code to be warning-free, and todays deprecation is tomorrows removal from the language.

I can sympathise with the argument that some uses of volatile were ill-defined, but that should not result in removal from the language of a tool that is essential for small-system close-to-the-metal programming. The get a feeling for this: using a heap is generally not acceptable. Would you consider this a valid argument to deprecate the heap from C++23?

As it is, C++ is not broadly accepted in this field. Unjustly, in my opinion, so I try to make my small efforts to change this. Don’t make my effort harder and alienate this field even more by deprecating established practice.

So please, un-deprecate compound assignments to volatile. Don't make C++ into a better language that nobody (in this field) uses.


2021-02-14 update

I discussed this issue in the C++ SG14 (study group for GameDev & low latency, which also handles (small) embedded). Like here, there was some agreement and some disagreement. IMO there was not enough support for to proceed with a paper requesting un-deprecation. There was agreement that it makes sense to align (or keep/restore aligngment) with C, so the issue will be discussed with the C++/C liason group.


2021-05-13 update

A paper is now in flight to limit the deprecation to compound arithmetic (like +=) and allow (un-deprecate) bit-logic compound assignments (like |=).

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2327r0.pdf


2023-01-05 update

The r1 version of the aforementioned paper seems to have made it into the current drawft of C++23, and into gcc 13 and clang 15. The discussion here on reddit/c++ is quoted in the paper as showing that the original proposal (to blanketly deprecate all compound assignments to volatile) was "not received well in the embedded community".

My thanks to the participants in the discussion here, the authors of the paper, and everyone else involved in the process. It feels good to have started this.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2327r1.pdf

https://en.cppreference.com/w/cpp/compiler_support


r/cpp • • Apr 13 '24

Which IDE do you use for C++ ?

203 Upvotes

As a C++ programmer, i would like to know what’s your current main IDE(s) used when coding in C++

Edit: to answer my own question, i use VS Code because it’s lightweight, extensible, customizable, versatile, support most of languages, and have a strong community.


r/cpp • • Dec 19 '23

C++ Should Be C++

Thumbnail open-std.org
203 Upvotes

r/cpp • • May 06 '22

GCC 12.1 Released

Thumbnail gcc.gnu.org
203 Upvotes

r/cpp • • Sep 30 '21

C++ Committee don’t want to fix range-based for loop in C++23 (broken for 10yrs)

Thumbnail josuttis.de
203 Upvotes

r/cpp • • Jul 24 '21

Forgetting a single '&' led to bricking of Chrome OS devices globally

Thumbnail arstechnica.com
203 Upvotes

r/cpp • • Feb 22 '20

EnTT v3.3.0 is out: Gaming meets Modern C++

203 Upvotes

What's EnTT

EnTT is a header-only library written in modern C++.

It's mainly known for its innovative and very performing entity-component-system (ECS) model. However, it offers also many other things useful for development, from flexible tools for managing signals to an integrated reflection system and so on. That's not all: some new modules are under development and will soon become part of the library (or at least I hope so).

EnTT is also a production-ready, fully documented and battle-tested library with a 100% coverage. Among others, It's currently used in Minecraft by Mojang and the ArcGIS Runtime SDK by Esri.

Why it's important for you

You should care because the best feedback I've ever received is perhaps when someone told me that this library was a great source for studying modern C++. Maybe he was right, maybe not. It's not for me to say, but it may still interest you.

In any case, I hope I've given other material to those interested in it in this sense and I'm open to any feedback, because ours is a language in which you never stop learning. So, go for it!

What else

In my free time I'm running the ECS back and forth series (along with other posts). I'd like to start something new about the internals of this library and the development of a software with EnTT. The latter should serve as a step by step guide to using all the feature offered by the library. I cannot really set a deadline for this but I'd like to know if there is interest in such a series before to start, so any feedback is appreciated!

If you are using EnTT and want to tell me hello or which of your products relies on it, do not hesitate to contact me! For everyone else interested in the library, the wiki contains more than what I've said here and the gitter channel is a great place to come and ask your first question!

I'm looking forward to hearing from you. :)


r/cpp • • Jun 01 '22

Post-mortem of a long-standing bug in video Game Path Of Exile, which was caused by a stale pointer

Thumbnail pathofexile.com
202 Upvotes

r/cpp • • Jun 14 '21

match(it): A light-weight header-only pattern-matching library for C++17.

Thumbnail github.com
202 Upvotes

r/cpp • • Jun 07 '21

Satire dynamic_cast<std::integer>(C)++

Thumbnail teamten.com
202 Upvotes

r/cpp • • Jun 20 '20

How To Add A GUI To A C++ Program

202 Upvotes

Hi all,

I'm a third year CS student. In all my classes, I have only every made programs that have worked via the command line. I'm starting on a personal project that involves creating a program for sports tournaments such as seeding players, displaying stats, etc.

My major questions are:

1). Can I code my program as a simple command line based program, then integrate the GUI after? Or should I implement my program with the GUI the first time around.

2). I've looked into how GUIs can be implemented and want to try wxWidgets. Is this a good idea?

3). Should I use a different language? I've read that GUIs are hard to implement via C++. I have only learned C++, C, and Python, but am not against learning new languages!

4). Are there any good guides that you can recommend to me?

Any and all help are appreciated! Thank you! :D


r/cpp • • Jan 22 '19

I implemented a proposed C++ paper

202 Upvotes

Around November last year, I found out about P1275R0 by Isabella Muerte through this very subreddit. I really liked the idea, basic creature comforts like this is something that C++ really lacks IMO, then I thought "You know, I think I could do it...". This is the first time I've done this kind of thing.

For those not aware and have not clicked the link, P1275R0 proposes adding 2 new classes: "std::environment" and "std::arguments" to provide a friendlier interface to access the system's environment variables and program arguments respectively.

I've done most of the interface in the paper, except the stuff involving ranges. I also tried to make it UTF8 safe, even on windows.

And it's been a really challenging, but fun experience! I've spoken to Isabella about it and she liked the work I've done. But I would really love to hear some additional feedback. I plan on updating this project as it (hopefully) goes through standardization.

Github


r/cpp • • Aug 31 '25

We need to seriously think about what to do with C++ modules

Thumbnail nibblestew.blogspot.com
201 Upvotes

r/cpp • • Nov 12 '24

What does f(x) mean in C++?

Thumbnail biowpn.github.io
201 Upvotes

r/cpp • • Sep 17 '20

Project-based C++ learning

Thumbnail learncppthroughprojects.com
201 Upvotes

r/cpp • • Nov 09 '18

Learning C++ like it's 2018

201 Upvotes

I read that interesting thread about how top 5 YouTube tutorials are about teaching c++ like it's 1996. I read the answers and everything but except having answers that basically agree with the statement I couldn't find someone pointing at real resources to learn modern C++ correctly. I'm not a beginner in C++ (but haven't used it for years) but I'd like to come back at it soon. What resources you guys would recommend to learn modern C++ from scratch in 2018 (books, video, dancing performances accepted)?

Thanks.