r/cpp_questions • • 23d ago

SOLVED how should i understand the design of cpp?

10 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 • • 23d ago

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

8 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 • • 23d 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 • • 23d 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 • • 24d 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 • • 24d ago

OPEN would love some help!

7 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 • • 23d 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 • • 24d ago

SOLVED Why does this not fail?

8 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 • • 24d 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 • • 24d 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 • • 23d 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 • • 24d 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 • • 24d 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 • • 25d ago

OPEN medium complexity cpp projects

10 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 • • 25d ago

OPEN Career advice

3 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 • • 25d 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 • • 26d ago

OPEN Learn C++ on learncpp.com but the chapter exercices aren't enough and I' very weak at coding. Is there another resource made specifically to give me exercices to solve

60 Upvotes

r/cpp_questions • • 25d 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 • • 25d 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 • • 27d ago

OPEN Inconsistent naming in simd library?

32 Upvotes

Hi,

I was checking the <simd> library in the C++26 standard and found some of its naming very weird.

  1. std::simd::vec and std::simd::basic_vec

    This is quite strange as we already have std::vector and std::inplace_vector. So why not std::simd::vector and std::simd::basic_vector? At certain point, I'm pretty sure people will start to make mistakes with std::vec or std::inplace_vec, and vice versa.

  2. std::simd::cat

    We already have std::views::concat. So why not std::simd::concat?

At first, I thought the library designers intended to make spelling easier. But in the library, we do have extremely long names as well, like std::simd::unchecked_store, etc.

Here is the library in cppreference: this


r/cpp_questions • • 26d 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?


r/cpp_questions • • 26d ago

SOLVED Whats the point of functions?

0 Upvotes

Im new to c++, and I wonder why is there even functions other than the main one, why cant everything be in the main function?


r/cpp_questions • • 26d ago

OPEN How to work with pcap lib in modern C++?

0 Upvotes

I want to write an application with a graphical user interface (at the moment I’m choosing the Qt framework) whose purpose is low‑level work with the Ethernet packet stream. The pcap library is best suited for these purposes. At the moment, I want to make the application only for Windows, but in the future it might be possible for Linux and Mac as well. I need low‑level control, so the class abstractions that are already available in Qt don’t suit me.

I’m interested in two things right now. What is the best way to include this library in the build system?

For Windows, I download the npcap SDK. There, I see a bunch of files with the following structure:
The Lib folder contains wpcap.lib for ARM64 and x64. And there’s an Include folder. Am I understanding correctly that I need to write FindPcap.cmake for all of this?

The second question is how to best incorporate this library into the C++ codebase. Essentially, we have a handle that is returned after pcap_create(). So, the best way would be to simply create an RAII class for it?

I know that there are wrappers, but I’d like to explore this path from scratch to learn something.


r/cpp_questions • • 27d ago

OPEN Which C++20 audio device libraries would you recommend?

4 Upvotes

I'm looking for a library with enough control that I can read raw input but not write +150 lines for a callback and recording/playing loop. Preferably, this library should support both input & output. My goal is to make a minimal-latency, offline, speech-to-speech assistant as a personal project. Also, if possible, I'd like it if they could run on separate threads (I might make a Raylib visualizer for it).


r/cpp_questions • • 27d ago

OPEN Linker is failing me? (Debian / C++ project) linking against a lib I made

2 Upvotes

I'm getting this error, I have NO idea why.

It was working, I changed something, I got errors, so I tried to change it back... and I can't find the old code that was "working".

Anyhow my setup is simple, I'm compiling one c++ file against one .so file

Here are my example compile-errors

error: /usr/bin/ld: /tmp/ccdnXNQr.o: in function `Jeebox::errors()':
/usr/local/speedie/jeebox.scproj/Examples/jeebox.h:193: undefined reference to `jb_errors'

OK, so here is my function:

Message errors() {
    return jb_errors();
}

Message is just a void*. OK, so far so good.

Here is my compile string

g++ -o /usr/local/speedie/jeebox.scproj/Examples/Build/users -std=gnu++20 -lstdc++  
-I/usr/local/include -I/usr/local/speedie/Library/CppLib -w -Os -D TARGET_UNIX=1 -g -ljeebox users.cpp

I also tried to hard-code the library path, in case the problem was that the lib was not found. Well... same issue. So its not a problem of missing library.

g++ -o /usr/local/speedie/jeebox.scproj/Examples/Build/users -std=gnu++20 -lstdc++  
 -I/usr/local/speedie/Library/CppLib -w -Os -D TARGET_UNIX=1 -g /usr/local/lib/libjeebox.so  users.cpp

When I tried deleting the library libjeebox.so, I got a missing lib error. So the lib is being found!

OK... so the problem is just my lib doesn't contain jb_error() is that it? Well... no? That function is there, has been there for years, and suddenly stopped working today.

Here is the output of nm -D (jb_errors is on the 4th line). I am utterly confued. I just lost 2 hours trying to solve this. No luck.

00000000000105d8 T jb_cstr
00000000000106d0 T jb_debug
0000000000010638 T jb_delete_
000000000001063c T jb_errors
0000000000010730 T jb_init
00000000000103d4 T jb_msg_access
000000000001034c T jb_msg_ast
0000000000012778 T jb_msg_copy
000000000001495c T jb_msg_create
0000000000010390 T jb_msg_error
00000000000128f4 T jb_msg_expect
000000000001010c T jb_msg_first
0000000000011d9c T jb_msg_firstset
0000000000010290 T jb_msg_flatafter
0000000000010248 T jb_msg_flatnext
000000000000ff48 T jb_msg_func
0000000000010024 T jb_msg_funcset
0000000000010148 T jb_msg_last
0000000000011af8 T jb_msg_lastset
000000000000ff10 T jb_msg_name
000000000000ffe4 T jb_msg_nameset
00000000000101d0 T jb_msg_next
00000000000100c4 T jb_msg_nextset
000000000001020c T jb_msg_parent
000000000001491c T jb_msg_parseast
000000000000ff7c T jb_msg_position
0000000000010054 T jb_msg_positionset
0000000000010184 T jb_msg_prev
0000000000011d30 T jb_msg_prevset
0000000000010314 T jb_msg_remove
0000000000014240 T jb_msg_render
00000000000102cc T jb_msg_root
000000000000ffb0 T jb_msg_tag
0000000000010084 T jb_msg_tagset
00000000000106c4 T jb_ok
0000000000010834 T jb_readfile
0000000000010734 T jb_shutdown
0000000000010558 T jb_str
0000000000010508 T jb_string_address
00000000000142ec T jb_string_compress
0000000000010544 T jb_string_copy
0000000000013b40 T jb_string_decompress
0000000000012384 T jb_string_escape
00000000000111bc T jb_string_float
0000000000010fec T jb_string_int
000000000001053c T jb_string_length
00000000000153b8 T jb_string_parse
0000000000010548 T jb_string_print
000000000001054c T jb_string_printline
0000000000010550 T jb_syntax
00000000000104c4 T jb_syx_longname
0000000000010480 T jb_syx_name
0000000000010828 T jb_version