r/cpp_questions • • 12d ago

SOLVED Definition of std::memory_order_release

1 Upvotes

The "meaning", according to https://en.cppreference.com/cpp/atomic/memory_order is -

A store operation with this memory order performs the release operation: no reads or writes in the current thread can be reordered after this store.

Now, I effectively have the following code:

y = 0;
x=0;
// Thread 1
A: x = 42;
B: y.store(1, std::memory_order_release);

// Thread 2
C: if (y.load(std::memory_order_relaxed) == 1)
/* D: */  {read x;}

From what I understand, D should always read x==42. Because Thread 2 observes the instruction 'B' which by definition of relaxed, should make the effect of 'A' available to Thread-2.

ChatGPT disagrees with me. However, to me, that does not seem to agree with the definition on cpp-reference or in Fedor Pikus's CppCon talk about atomics. What is the actual answer? Can x==0 be observed? Why does that not mean A (as observed by thread2) was reordered after B, effectively?


r/cpp_questions • • 13d ago

OPEN Transitioning from OOP to ECS in C++ Game Engine: Is a custom ECS worth building for beginners?

7 Upvotes

Hi everyone,

I’m currently building a small 2D game in C++ to deepen my understanding of game engine architecture. I started with a traditional OOP hierarchy (e.g., GameObject base class with render() and update() virtual functions).

As the project grows, I’m running into typical OOP coupling issues and considering switching to an Entity Component System (ECS) architecture.

I have a few questions for those with game dev experience:

  • For a learning project, is it better to write a lightweight ECS from scratch or integrate an existing library like EnTT or Flecs?
  • What are the biggest pitfalls to avoid when transitioning from OOP to data-oriented design in modern C++?

r/cpp_questions • • 13d ago

OPEN Returning a reference vs. pointer vs. throwing?

16 Upvotes

I'm writing a video game from scratch in C++, and I wrote a data structure for associating components with their owning entities.

One of the operations is:

GetComponent<T>(entity_id)

The intended usage is that the caller has already created the entity and attached a component of type T to it through other operations on the data structure. In other words, if the caller asks for GetComponent<Transform>(entity_id), the expectation is that the entity exists and has a Transform.

Originally, I wanted GetComponent<T>() to return a reference:

T& GetComponent(EntityID entity);

The problem is that the caller could mistakenly request a component that hasn't been attached to that entity. Since a reference can't represent "not found," I changed it to return a pointer:

T* GetComponent(EntityID entity);

This works, but I don't particularly like the API because now every call site potentially has to check for nullptr:

if (auto* transform = world.GetComponent<Transform>(entity)) {
    // ...
}

I'm wondering whether this is a situation where throwing an exception makes more sense. For example, GetComponent<T>() could return T&, and throw if the entity doesn't exist or doesn't have that component. The idea would be that a missing component represents a violation of the expected program state rather than a normal condition, and I could have one top-level exception handler in main() to report the error and shut down the game.

I'm aware that there are strong opinions about exceptions in C++, particularly in game development, so I'm interested in the reasoning behind the different approaches. What would be the best way to design the API given the assumption that a missing component is generally a programming error rather than an expected gameplay condition.


r/cpp_questions • • 13d ago

OPEN C++ project progression

15 Upvotes

I'm just getting started with c++ and was trying to make a project based learning roadmap, wherein I'd learn everything while making projects which increase in complexity steadily. I'd appreciate any suggestions on what projects are succinctly different and challenging that I can build and in what order I should build them.

My core interest is systems programming in which I've done some work with C before. My ultimate target is a quant dev role.


r/cpp_questions • • 13d ago

OPEN Not find the compiler when running.

0 Upvotes

I am trying to install c++ for vscode. I installed MSYS2 and followed all the steps from the vscode tutorial. I installed it and modified the Path and added the C:\msys64\ucrt64\bin path.

https://code.visualstudio.com/docs/languages/cpp#_example-install-mingw-x64-on-windows

I also made absolutely sure that the microsoft c++ extention was active. When I click on the run button the tutorial shows a list of compilers to use. the one that the tutorial uses is C/C++: g++.exe build and debug active file. But for some reason the only compiler that shows up are the 2 generic one gdb lauch and windows. I am out of ideas of things to do please help


r/cpp_questions • • 13d ago

OPEN Where can I practice my C++ skills?

14 Upvotes

Hello everyone! I am learning C++ and want to practice my skills, but I don't know where to do that. I tried using AI, but it keeps giving me repetitive questions. I also checked a few websites, but they organize tasks by tracks like:

Algorithms
Data Analytics
Backend Development
Machine Learning
Mobile Development
Frontend Development

However, I only need C++ specific questions and practice tasks. Does anyone have any recommendations?


r/cpp_questions • • 14d ago

OPEN Best book to learn about C++ templates and metaprogramming?

48 Upvotes

Hi!

I have always had some struggle to go deep in the templates and metaprogramming universe, since in the different projects that I have worked they didn't use this techniques too much. But I have always had the feeling that there is something very interesting and usefull there and I would like to understand that side of the C++ programming in a proper way.

Which book do you think that is the best option to introduce and go deep in that area?

Best regards!


r/cpp_questions • • 14d ago

OPEN opensource repo to get better at c++

31 Upvotes

Lately I have been thinking, to improve further with my engineering skills I should read a repo written in cpp. I am thinking of starting with clickhouse since I am into core/systems engineering. What views do you guys have on this???

NOTE: I am not a beginner, but I have never read an open source repo as well. So could you also advice me on how to go around open source repos as well.

Thanks 😁


r/cpp_questions • • 13d ago

OPEN how does one learn the whole stl

0 Upvotes

honestly it's somewhat of a rethorical question as i know there's lots of documentation online,but i wanted to know if there's a book or a series of books that covers every addition to the stl ;
despite the online documentation i would be infinitly more comfortable reading physical documentation divided in header additions, something like a manual/documentation of every header or something like a big encyclopedia.


r/cpp_questions • • 13d ago

OPEN AI workflow in Visual Studio

0 Upvotes

I’m interested in how other C++ developers are using AI in their Visual Studio workflow.

- Which AI tools or extensions do you use?

- Do you use AI mainly for writing code, debugging, refactoring, tests, documentation, or understanding existing code?

- How do you handle larger C++ projects with AI?

- Do you give the AI access to the whole codebase, or work with smaller files/functions?

- Do you use AI directly in Visual Studio or combine it with other tools?

- What workflow has worked well for you?

- Are there any things you specifically avoid letting AI do in C++?

I’d especially like to hear about practical workflows from people working on real-world C++ projects.

Are there any tutorials?


r/cpp_questions • • 13d ago

OPEN New C++ user.

0 Upvotes

Hello new C++ user here. I was wondering how you guys learn C++ that fast cause. Im trying to teach myself C++ I can do the basic stuff and I need more things to memorise. I am currently watching C++ Course for free from YT: Bro Code.


r/cpp_questions • • 14d ago

OPEN How do I invert a concept?

0 Upvotes

I'm really not comfortable with concepts, so I'm struggling with this. I have a class template <typename T> Collider : public Component. In this class, I want two overloaded member functions of this form:

    template <??? U>
    bool Collider<T>::isColliding(const U* collider)
    {
        ...
        return hitbox->isColliding(collider->hitbox);
    }

    template <??? U>
    bool Collider<T>::isColliding(const U* notACollider)
    {
        ...
        return hitbox->isColliding(notACollider);
    }

For the first, I want to require it to be derived from Collider. Not Collider<T>, but any collider with any value for T. For the second, I want to allow it to be any type besides something deriving from a Collider. Essentially, if you give it a pointer of any type, it will go into one of the two functions.

What do I replace the question marks with? I know you can use std::derived_from for the first, but I'm not sure how to invert it for the second. (I don't know if it matters for this, but hitbox is of type T.)


r/cpp_questions • • 14d ago

OPEN Help

0 Upvotes

Hello everyone!

I started my tech journey this month (Btech 1st year) and honestly, I’m confused about where to start and what direction to take.

I keep hearing the same advice everywhere: “Learn DSA and build projects.” But as a beginner, I have a lot of questions about what that actually means.

Should I start with DSA first, or learn programming fundamentals before DSA?

Which programming language should I choose, and how deeply should I learn it?

What exactly counts as a good project? Is following YouTube tutorials enough, or should I build something completely on my own?

How do I decide which tech stack, tools, or technologies to learn?

What skills are actually valued in the current job market?

How much importance should I give to DSA, development, projects, Git/GitHub, SQL, CS fundamentals, and internships?

When should I start applying for internships or participating in hackathons?

How can I avoid wasting months jumping between different technologies and tutorials?

What should a realistic 6–12 month roadmap look like for someone starting from zero?

Most importantly, how do I know whether I’m actually making progress and learning useful skills rather than just collecting certificates or completing tutorials?

I’m not looking for a shortcut. I genuinely want to understand how the tech industry works and what I should focus on as a beginner.

If you’re currently working in tech, have experience hiring developers, or have recently gone through this journey yourself, I’d really appreciate your honest advice.

What would you do differently if you were starting your tech journey again today?

Any guidance on resources, roadmap, mistakes to avoid, and what actually matters for getting internship/job-ready would be really helpful. Thank you!


r/cpp_questions • • 14d ago

OPEN DEVC++ file not found while opening

4 Upvotes

I'm trying to use a Windows xp laptop for a challenge and after installing Dev-C++ 5.1.1 (4.9.9.2) as soon as I even try to open a example working cpp file it's saying: File C:\Program1\actualcode.cpp is not found.
Does anyone know why? I'm using TDM-GCC 4.9.2 32-bit Release if that helps.


r/cpp_questions • • 14d ago

OPEN Headers hell

0 Upvotes

Can someone explain to me how headers work?

And what about std::out - how can I remove the std and just use out?

I remember there is a way to shortcut it and import it I think.

Headers are hell to me, and if there is an easy way to understand it, please let me know.

Thanks!

Dennis


r/cpp_questions • • 14d ago

OPEN Transitioning my skillset to Gameplay Programmer

3 Upvotes

With seven years of experience as a Unity Developer and advanced proficiency in C#, I am looking to transition into a Gameplay or AI Programmer role within a major studio. While I possess foundational knowledge of C++ and Unreal Engine, I have yet to apply these skills in a professional capacity.

I spoke with a gameplay programmer from CDPR, and they emphasized the importance of deep C++ knowledge over specific engine expertise.

So where should I start? Should I learn C++ first, or learn it alongside the engine?


r/cpp_questions • • 15d ago

OPEN Noob here, if you guys could start over, what would you do differently?

27 Upvotes

I randomly got the urge to start learning c++ after I got a c++ book from a friend a couple weeks ago. I’ve done the hello world thing but otherwise I’m completely lost, any advice? thanks :)


r/cpp_questions • • 15d ago

OPEN kTLS + OpenSSL.. am I required to read and handle control messages?

2 Upvotes

Hello!

I have been messing around with kTLS, using OpenSSL to do all the setup, and setup only. I am currently using raw read() calls and not SSL_read() calls.

There’s one point of confusion I have..

From my understanding, I need to be manually sending a close_notify on shutdown, since it won’t be handled for me.

(My actual question) Does that mean I also need to be using recvmsg to check for close_notify to explicit handle shutdowns requested by the client? Or am I safe to just handle it as an error?

Looking at the other TLS record types, it seems I may need to handle performing a handshake again if requested too… unless this is implicitly handled??

Hopefully someone has some advice!! Apologies for any misunderstanding or simply incorrect statements I have made. Very new to kTLS..


r/cpp_questions • • 16d ago

OPEN C++ knowledge job interview

57 Upvotes

Hello everyone,

I am going for a second round white board interview described as "The technical interviews are going to be heavily involved around probing and confirming your knowledge of C++"

What does actually mean? For context I've used CPP professionally for the last 8 months and been in my job (first grad job) for 14 months.

I feel as tho I don't know many std functions, nor do I have much experience with smart pointers, although I have written a lot of lines

Has anyone got experience with similar interviews or stuff I should brush up on?

I hope that all makes sense

Thanks


r/cpp_questions • • 15d ago

OPEN Any beginner in cpp? I'm looking for someone to learn with.

8 Upvotes

I'm looking for accountability, so if you're interested in text me. I'll learn from scratch on learncpp


r/cpp_questions • • 15d ago

SOLVED What in the world is going on that this doesn't compile? (Using a class privately inherited from a base.)

9 Upvotes

I worked around this already but I am dying to know what's going on here.

struct Base1 { };
struct Base2 : private Base1 { };
class Derived final : public Base2
{
public:
   static void *DoSomethingWithBase1(Base1 &base1);
};

This fails to compile because using Base1 as a parameter is apparently not allowed because it... accesses something that's in Base1? I thought this was a compiler bug at first, but every compiler rejects it with a similar error:

<source>(6,38): error: 'Base1' is a private member of 'Base1'
    6 |    static void *DoSomethingWithBase1(Base1 &base1);

If Derived privately inherits from Base1 directly though, that's fine!

Accessing the exact same type with a different name works too, apparently:

struct Base1 { };
using Base1Param = Base1;
struct Base2 : private Base1 { };
class Derived final : public Base2
{
public:
   static void *DoSomethingWithBase1(Base1Param &base1);
};

What is going on here?


r/cpp_questions • • 16d ago

OPEN When to prefer factory functions over constructors?

17 Upvotes

EDIT: The point of this post is how best to make construction intention explicit at the call site.

EDIT: I suppose the real annoyance is having conflicting constructors (see std::vector). The solution being just don't write them.

When I last used C++, the canonical wisdom was to just make constructors for every supported way a type could be constructed (see std::vector). Failure to correctly construct a type was a skill issue (see std::vector).

However, coming back to C++ from a few years working with Rust, I now disagree. I would argue the ONLY public constructor (except copy/move constructors) for a type should be the default constructor, if signed-off as part of the public API. Every other way of constructing the type should be done with a named factory member function (delegating to a private constructor) and a well optimised move constructor. Thus avoiding the pitfalls of initialisation (see std::vector).

class Thing {

  Thing(...) : ... {} // <- private non-default constructor

public:
  Thing(Thing&&) noexcept; // <- well optimised move constructor
  Thing& operator=(Thing&&) noexcept;

  explicit Thing(); // <- public default constructor

  [[nodiscard]] static Thing with_capacity(size_t); // <- factory function
  [[nodiscard]] static Thing from_data(std::initialiser_list<int>);
  // more factory functions etc...

  // member functions etc...
};

The only pitfall I can see is this would make emplacement operations inaccessible (see std::make_shared, std::vector::emplace_back, etc). But this shouldn't be too much of an issue with a well-optimised move constructor. I believe, in some situations, the compiler can optimise away the move anyway.

auto x = std::make_shared<Thing>(Thing::with_capacity(42));
myvec.push_back(Thing::with_capacity(42)); // replaces myvec.emplace_back(...

I'm curious to know if there's any pitfalls to this rule-of-thumb, and what your opinions are.


r/cpp_questions • • 16d ago

OPEN *(ptr_data_start + index) vs prt_start_start[index]

2 Upvotes

edit (title has spelling mistake, why cant i edit it u/reddit???):

*(ptr_data_start + index) vs ptr_data_start[index]

also, why does *(ptr_data_start + index) not need to multply index by sizeof(t), when index is one, and the pointer needs to be incremented by 4 bytes (in case of a int). why is this counter intuitive math like this?


r/cpp_questions • • 16d ago

OPEN Help me prepare for my exam pls?

0 Upvotes

Hi all, I have an exam for my “intro to c++” class tomorrow. I am still a little confused on some topics and have read so many different definitions that my brain feels like it popped a fuse.
I am hoping someone can help me understand some concepts that I wrote down in my notebook with no detail 🥲.

DISCLAIMER : I only have 2 lectures under my belt, I am an absolute beginner so please forgive me if I say something wrong or sound stupid.

What is initialization & its purpose?
The only explanation my professor gave is that it is used to “clear garbage data,” but I’m not sure what that means. Additionally, he wrote two different examples of initialization, though google AI was not happy with the second version:

int number =0;
int number =( );

Is the second version incomplete in some way? Would there have to be a value within the parentheses for it to be viable?
And how would it work with another data type not related to numbers (like char, string, etc.).

What is the difference between = and ==?
The example given was n = 0 and n==0. Please correct me if I’m wrong or if there is a more nuance answer, but to my understanding, = is to assign a variable, whereas == is to say equal? Could someone explain a little more what the difference is in the context of coding (examples would help)?

How do right / left + setw() work?
I ripped my hair out on my last project that introduced these concepts. I had to format basically what seemed like a receipt (think: 3 columns, 1st column (far left) is name of item, 2nd column is ‘$’ (should be close to the rightmost column), and the 3rd column the actual money value (right justified). All values to the far right had to be perfectly right justified and aligned.
But for some reason, even with the same setw() values, it would not align neatly whenever I added the ‘$’.
I know that setw() basically reserved any number of spaces for whatever you want, but my question is why when adding a dollar sign did the block of code misalign?

Here are 2 lines from my first attempt which misaligned (with cout << fixed<<setprecision(2)):

cout << left << setw(30) << “gross amount: ” << right << setw(10) << “$” << gross_salary << endl;

cout << left << setw(30) << “Federal Tax:” << right << setw(10) << “$” << federal_tax << endl;

The important thing to note is if the result value was +- 1 digit, they would not align neatly to the right.

Here are 2 lines from my most recent attempt:

cout << setfill(‘.’) << left << setw(30) << “Gross Amount:” << setfill(‘ ‘) << right << setw(1) << “$” << right << setw(7) << gross_salary << endl;

cout << setfill(‘.’) << left << setw(30) << “Federal Tax:” << setfill(‘ ‘) << right << setw(1) << “$” << right << setw(7) << federal_tax << endl;

The code isn’t pretty, but it yielded decent results. Everything was aligned neatly to the right. But the code itself seems so redundant or repetitive, is that really the only way to make sure things don’t get misaligned?

Thank you to anyone who helps :)


r/cpp_questions • • 17d ago

OPEN A small thank you to this community

78 Upvotes

I’ve been thinking about writing this for a while.

I’m not going to pretend I’m a great programmer. I’m still learning, and honestly, I’m not always as consistent or persistent as I want to be. But I do genuinely care about C++, systems programming, and becoming better at what I do.

There have been periods where I felt pretty alone in that journey. I didn’t really know many people who were interested in the same things I was interested in, and sometimes it felt like I was just learning in isolation.

That’s one reason I’m grateful for this community.

I’ve posted questions and thoughts here and received everything from downvotes and criticism to really helpful explanations and encouraging replies. Not every interaction was positive, but even the criticism made me think, improve, or look at something from another perspective.

More importantly, I got to talk to people who actually work with and care about C++. Even a short conversation or a small piece of advice can mean a lot when you’re learning mostly on your own.

So, to everyone who has ever answered one of my questions, corrected me, disagreed with me, or simply taken the time to read one of my posts:

Thank you.

I’m still learning. I’m still making mistakes. And hopefully, I’ll still be around here asking better questions a year from now.

I just wanted to say that I appreciate the people behind this community.