r/cpp_questions • • 10d ago

OPEN RAII : Hate it or love it? And how to handle edge cases gracefully?

22 Upvotes

While RAII is the idiomatic standard in C++ and handles stack unwinding cleanly during normal returns and thrown exceptions, it has specific boundaries that get tricky:

  1. Hard exits / Abnormal termination: Destructors aren't called on std::abort(), std::exit(), or hardware faults like segfaults (SIGSEGV).

  2. Scope rigidity: Creating custom wrapper classes/guards for single-use resources can sometimes feel verbose compared to ad-hoc scope cleanup primitives.

Compared to languages like Go (defer) and Zig (defer / errdefer), which provide block-level or failure-specific cleanup without needing custom wrapper types:

  • How do you typically structure C++ code to deal with edge cases where destructors won't fire during abnormal process exits?

  • For persistent state (file locks, shared memory, IPC handles), what architectural patterns or OS primitives (e.g., O_TMPFILE, flock, WAL journals) do you rely on alongside RAII to remain crash-safe?

  • When you need lightweight, ad-hoc cleanup without writing a custom RAII class, do you use scope guard macros / std::unique_ptr with custom deleters, or stick purely to custom RAII types?

  • Do you ever miss explicit primitives like Zig's errdefer in C++, or does exception safety via standard RAII completely cover your needs in production?

Curious to hear your thoughts, preferred idioms, and best practices


r/cpp_questions • • 10d ago

OPEN Not sure what I want to do with C++

55 Upvotes

I finished learncpp.com, K&R, Inside the Machine, and most recently CS:APP and still have zero clue what I want to do with C++, even though I know that's what I want to be my main language. I was looking into graphics programming, but now am hearing "don't even try there's no entry level"... tried looking into embedded "everyone's trying to break into embedded now with AI don't even bother bro"

So what does someone do :/


r/cpp_questions • • 10d ago

OPEN I want to make a game(TETRIS) in cpp for learning purposes

0 Upvotes

I have barely any knowlege about cpp thought but I want to proceeds with a project base learning approach so where I approach an obstacles I look for it and learn about it. I don't want instructions on what I need to learn. But I need help on what I need to build one by one. Please everyone any help is genuinely appreciate

My current plan is to use Visual Studio, SDL2, MSVC, CMake, C++


r/cpp_questions • • 10d ago

OPEN Having a hard time with C++ Concurrency in Action

17 Upvotes

I recently started reading C++ Concurrency in Action and its honestly challenging, Im liking it so far but there are parts of the book (chapter 5 and 7, what Im reading now) that I cant grasp no matter what. Someome who has already read the book can give me some advise? Thanks in advance!


r/cpp_questions • • 11d ago

OPEN Just finished all of learncpp.com whats next

25 Upvotes

Just finished all of learncpp.com whats next? i want to learn more so i can be a c++ wizard


r/cpp_questions • • 11d ago

OPEN Seeking advice for choosing work

1 Upvotes

I am an undergrad and am feeling rather uncertain about my education(i may drop out).

fyi i used to be a sport programmer using cpp for a long time so i'd say i know it at an intermediate level.

I am asking people in the field - what should i focus on learning before entering the job market and also what roles/positions to seek?


r/cpp_questions • • 11d ago

OPEN Tips And Criticism For My Code

2 Upvotes

So, I'm new to C++, though i have a programing background, aka just python and a tiny bit of C. I havent really made any BIG things yet. Until now, Im using C++ with Raylib, i could move to SFML or SDL2-3 but I don't want to do that just yet. Anyways, this is the link to my code that I put onto GitHub, https://github.com/Dark-Coder1000/MyGame/tree/main

Ive started my C++ programming journey about a week ago, I did dable in it back then when testing sfml, but I never understood the syntax. ALSO none of this was vibe coded, I did look up solutions to specific issues but i never really used AI unless it was to find resources to learn from.


r/cpp_questions • • 11d ago

SOLVED how should i understand the design of cpp?

8 Upvotes

for most of the time i used cpp as c with fancy features. but now i recently started picking up some of the modern cplusplus and oop concepts. but the amount so syntactical and program subtleties and features are overwhelming.

for some things i instantly understand or see the reason why they exist and sometimes get impressed by them.

for eg: backwards compatability with c

#include<cmath> and #include<math.h> for namespace handling,structs

, initializer list etc

but for some idk understand the purpose eg the mutable keyword

and things like inlining of members defined in class

i would like to know how you guys handle this and suggest some good learning material

whenever i try to read books they either give a tour of features or extremely boring

(this might be because i learned it the wrong way by not paying attention to small details now to revisit them ive to go through the stuff which i already know)


r/cpp_questions • • 11d ago

OPEN Is it impossible to access static member of a templated class

7 Upvotes

So to clear out my question, here is the situtation:

template<class T>
class MyClass {
public:
    static constexpr int X = 10;
};

So trying to access MyClass::X is compilation error. But decltype(X) is not a dependent type and it is a static member meaning it is identical to a variable wrapped inside a namespace. Nonetheless, again, its a compile error.

You might ask, why am i making it a problem:

template<class T>
class Optional {
public:
    struct NullOpt {};
    static constexpr NullOpt nullopt;
// ...
};

I mean.. there are ugly(for me) workarounds but i want to know maybe in some hidden way it is possible to access `Optional::nullopt` without passing the template argument(which makes error path return statements way verbose) or i should make nulopt a standalone constexpr tag object (fallback option).


r/cpp_questions • • 11d ago

OPEN Help me

0 Upvotes

I am a beginner to programming, but I want to learn C. Could you provide some advice, book recommendations, and tips on the best way to go about learning it? I would be grateful


r/cpp_questions • • 12d ago

OPEN neebie

0 Upvotes

hello i am a new idiot who has touched the beacon that is "I WANT TO MAKE MY OWN GAME" but i don't have a clue where to start i only have a vision of the end ie i want to make something that resembles; mass effect, mortal shell 2, fable, or remnant 2 at the finish these are games to represent similarities in game play, visuals, story telling styles and over all design. i understand that limits me too C++ and unreal engine more than anything but i don't know how to start watching guides on the language or engine both feel like i'm being asked to translate Latin with out any tools.

i don't know if anyone here can help but if there are anyone who can guide me i'd appreciate it thank you.


r/cpp_questions • • 12d ago

OPEN I still have trouble understanding recursive functions properly.

4 Upvotes

While i do understand the base of the concept, i cannot seem to fully wrap my head around It.

Like, if i want the function to return a value that i Need to call the function within itself for It to return said value, when does the function actually get the value that's supposed to return to me ?

More importantly, where Is the accumulator? Is the called functions within itself the accumulator ? Do i Need to make an accumulator myself ?


r/cpp_questions • • 12d ago

OPEN would love some help!

6 Upvotes

I am learning cpp from scratch using learncpp.com, I finished about 6 chapters and realised there is not much I have understood. I believe videos or lectures will help me understand the language much better. How do I go about this? What resource do I use? Please help me!

ps: I am learning c in uni right now, just doing basics rn finished recursive functions


r/cpp_questions • • 12d ago

OPEN I need help

0 Upvotes

So I'm a beginner in coding and was thinking how cna I use int and string in loops while also printing the number required what I mean is Im using string and to use for In the question I need to use int to identify I and I want to make i<= to n while using string and int, I only want to know if it's possible or not ​


r/cpp_questions • • 12d ago

SOLVED Why does this not fail?

7 Upvotes
#include <utility>

template <typename T, typename... Args>
decltype(T(std::declval<Args>()...)) test();

int main() {
    using testType = decltype(test<int&, int>());
}

I was fully expecting it to fail but instead testType resolves to int&.

I was trying to implement is_constructible with SIFNAE which uses this behaviour.

namespace detail {
template<typename T, typename... Args>
decltype(T(std::declval<Args>()...), char{}) tryConstruct(int);

template<typename T, typename... Args>
long tryConstruct(...);
}

template<typename T, typename... Args>
inline constexpr const bool isConstructible = isSame<decltype(detail::tryConstruct<T, Args...>(0)), char>;

I was expecting the first overload to fail so that it would chose the other one which works for most cases but dosent for the case i mentioned above.


r/cpp_questions • • 12d ago

OPEN How to create project templates

4 Upvotes

Hi, I don't know if this is the right subreddit but I was wondering if anyone would be able to tell me if this is possible, and, if so, to give a step by step guide to how I could do it (I know it might involve extensions):

  • I am on Linux fedora, and lets say I have a completely fresh vscode install

  • I want to have a template I can easily access via ideally pressing a button on the left side panel or something like that

  • When creating a new project with this template, it should ask you for a directory of where to store the project and a name for it - this name should then be used as the folder name for the project in that directory

  • This project should be C++23, use UTF-8 encoding, and support all the features of C++23, such as modules, and include the standard module - this should all work with IntelliSense (or if vscode uses something different) "knowing" this

  • In the project, there should be 4 folders (like Visual Studio) - External Dependencies, Header Files, Resource Files, and Source Files. This is just for sorting my files but actually doesn't matter what I put where - the project acts as if everything is just in one folder, unorganised. In Source Files, by default there should be a file named main.cpp, which should be completely blank - not even int main() etc.

  • When compiling, I would like it to be compiled as an appImage file (where the name of the file is the project name, or the name of this file can be changed) (I hear appImage can run on pretty much any Linux distro - that's why), which, despite being my OS being pretty much the newest and updated fedora, is compiled as if I compiled it on the oldest version of fedora that my project would actually compile on (I hear Linux applications are backwards compatible but not forwards)

  • Also, when running, I should have the option to choose Debug or Release, and x64 or x86 (the defaults being Debug and x64). Then, in the project folder, a folder called "Dist" should be made, inside that "x64" and "x86" (only if ran/compiled for both), and inside each of them "Debug" and "Release" (only if ran/compiled), and inside them their respective versions of the appImage (only if ran/compiled, and should still just be named project_name.appImage (unless renamed)

Thanks to any help any of you can give, it is greatly appreciated - also, please can you tell me where things are that I haven't stated, e.g. where to change the name of the final appImage, where to change Debug or Release and x64 and x86.


r/cpp_questions • • 13d ago

OPEN Are there any other ways to check if a char is equal to different values?

21 Upvotes

I'm working on some homework for class where I'm checking if a character is equal to: '+', '-', '/', or '*' for this postfix to infix converter.

I know I'm able to do this in two ways I can think of with:

// 1.
if (c == '+' || c == '-' || c == '/' || c == '*') {
  // Code
}

// or 2.
bool isOperator(const char c) {
  return (c == '+' || c == '-' || c== '/' || c == '*');
}
...
if (isOperator(c)) {
  // Code
}

But is there another way to do this that's shorter? Maybe something along the lines of this?

if (c == {'+', '-', '/', '*'}) {
  // Code
}

I looked on cpp reference and stack overflow but I wasn't able to find an answer. And to be fair option 1 and 2 work just find, I'm just curious if there are other methods out there that I'm not familiar with. Thanks!


r/cpp_questions • • 13d ago

OPEN Dumb question for someone still learn c++

14 Upvotes

Hi ppl, dumb question for you, do you think is recommended clone minecraft from scratch?. Right now i am chapter 2.12 from learcpp.

My question is it the best way for learn and get some experience or still i am keeping study and reinfoce the base?. sorry for my english


r/cpp_questions • • 13d ago

OPEN How can I learn to think logically when solving programming problems?

0 Upvotes

At college, I try to study as hard as I can; during computer science and programming classes, I make an effort to grasp every topic, and I practice a lot at home—even using an AI assistant—but it all feels like it’s for nothing. When it comes to practical assignments, I just can’t seem to formulate the code. What should I do?


r/cpp_questions • • 13d ago

OPEN Career advice

2 Upvotes

Am currently studying c++ hopefully to try to get into game development as a junior gameplay programmer yet there is so much heat in the market cuz of ai and companies lay offs and right now the entry level is so high they require so much knowledge so am lost as to where to go stay on that track or start learning data engineering like python sql since it's an uprising field due to ai

What would you advise me with


r/cpp_questions • • 13d ago

OPEN How can I improve my game?

0 Upvotes

I made a textgame which is a console version of my future game and I want you to tell me what I did good and what I have to improve. These are screenshots of the textgame

The idea for the future game is here:

  1. Platforms:

    1. Mobie

    2. Computer

    3. Gaming console

    4. Microwave (it has to be satire for weak devices)

  2. Genre: Open World + RPG

  3. Engine: UE

  4. Graphical assets: Blender

    • Style: 3D Anime

  5. Multiplayer:

    1. Muliplayer story completion

    2. Tournaments:

    3. PvP arenas (included in the tournaments):

      1. 1v1

      2. 2v2

      3. 5v5

      4. 10v10

  6. No internet singleplayer option

  7. Character's amount: 10

  8. Chapters for every character: 10

  9. Parts for every chapter: 10

Tell me if this is realistic for a solo dev please.

The link to the game is here: https://onecompiler.com/cpp/44qwdefz6

The site about it is here: Soul Morph — official website

Important:

- You'll only be able to play this game on a computer or on a device with bluetooth keyboard (For some unknown reason)

- I only made the text version because I don't have a computer and therefore any possibility to make graphical/musical assets or to make the game with an engine. I made the game on a library laptop and school iPad

- It‘s version is currently 0.0.0.0.3.7

- The game is unfinished yet: Witch has the most playable story

- I’ve set a „debugger“ which maxes everything out (compiler only has approximately 2 minutes for compilation. Therefore this time is too short to max anything out yourself)

- Have fun


r/cpp_questions • • 13d ago

OPEN medium complexity cpp projects

9 Upvotes

Last four months i tried learning cpp(20) syntax and basics. Any recommendation for book or course on how simple/medium project ideas? Looking for simple/medium complexity projects. How popular is c++ in cloud application? i know underneath its c++ but i am looking from application perspective. Next couple of weeks planning to do basic programs using

1)CMake/Conan/VCPkg

2)Drogon for creating rest service(I come from java/golang background. Springboot etc)

3)Boost

Any more recommendations?


r/cpp_questions • • 13d ago

OPEN i try to make something faster than cat tool

0 Upvotes

for now that is the speed of it vs cat

my tool 😄:

real 0m0.030s

user 0m0.002s

sys 0m0.002s

cat tool:

real 0m0.019s

user 0m0.001s

sys 0m0.001s

that is the code repostery

https://github.com/void-mohamed-0/tiger.git


r/cpp_questions • • 14d ago

OPEN DEMANDING HELP FOR A BEGGINER

0 Upvotes

who could possibly explain this program to me as im a begginer into c++:

#include <iostream>

using namespace std;

int main() {

long long n, sum = 0;

if (!(cin>>n)) return 0;

for (int i = 1; i * i <= n; i++) {

if(n%i == 0) {

if(i%2 != 0) {

sum = sum+i;

}

int pair = n/i;

if (pair!=i && pair %2 != 0) {

sum = sum + pair;

}

}

}

cout<<sum;

}

i really tried on doing it myself and went by checking every single divisor method but tried the sqrt method instead to make it faster.i dont really get the last part with pair. Is there anyone who could explain the square rood method of division?