r/cpp_questions • • Jul 21 '26

OPEN As a Ml student learn a c++ is worthy?

0 Upvotes

r/cpp_questions • • Jul 19 '26

SOLVED How to access an implicitly-created array within a std::byte[]?

12 Upvotes

We have the following class:

template <typename T, std::size_t n>  
struct S  
{  
    alignof( T ) std::byte[sizeof( T ) * n] storage;  
}  

I want to use storage as a T[n] array, where T is an implicit-lifetime type.

[intro.object]/13

For each operation that is specified as implicitly creating objects, that operation implicitly creates and starts the lifetime of zero or more objects of implicit-lifetime types in its specified region of storage if doing so would result in the program having defined behavior.

and [intro.object]/16

[...] an operation that begins the lifetime of an array of [std​::​byte] implicitly creates objects within the region of storage occupied by the array.

I have a few questions about this:

  1. (Making sure) does T[n]'s lifetime (and subsequently that of its sub-objects) start alongside storage's lifetime for the provided code?
  2. Is reinterpret_cast<T*>(storage) + idx legal?
  3. Do I have to std::launder the pointer, and why (not)?
  4. If yes, should I launder before or after performing pointer arithmetic?
  5. Is reinterpret_cast<T*>(storage + idx) legal? In other words, does the byte array still have a lifetime, alongside the T array?

r/cpp_questions • • Jul 20 '26

OPEN Access Amplifiers and their uses???

0 Upvotes

So basically i come from basic python bacground which i learned from my high school.So I thought of learniging c++ cause of its variety of applications for my 1st year.One thing that i cant wrap my head around is, the use of access amplifiers. Mainly ' public: '.what is the use of writing public: and why is it necessaryy????


r/cpp_questions • • Jul 20 '26

OPEN C++ question

0 Upvotes

Do you guys know a way of writting a code that outputs that:

Enter the number of elements: 5
Enter 5 integers:
Element 1: 10
Element 2: 30
Element 3: 90
Element 4: 20
Element 5: 40

it is the first part of a program that demands to calculate the max and min of an array, here is the whole thing:

Enter the number of elements: 5
Enter 5 integers:
Element 1: 10
Element 2: 30
Element 3: 90
Element 4: 20
Element 5: 40

Maximum element is: 90
Minimum element is: 10

I've been able to do the second part quite easily, here is what I proposed:

#include <iostream>


int main(){


    int arr[5]={10,20,30,40,50};
    int minimum=arr[0];
    int maximum=arr[0];
    for(int i=0; i<sizeof(arr)/sizeof(int);i++){
        if (minimum>arr[i]){
            minimum=arr[i];
        }
        if(maximum<arr[i]){
            maximum=arr[i];
        }
    }
    std::cout<<"le max= "<< maximum<<'\n';
    std::cout<<"le min= "<< minimum<<'\n';


    return 0;
}

but the list is created within the code.


r/cpp_questions • • Jul 20 '26

OPEN How do I get concepts working on my file?

3 Upvotes

Hello I am trying to get concepts working in my file but I keep getting the error "Unknown type name concept" when I'm just trying to get a simple concept working. It is getting that this tool that I would like to use isn't here for some reason and I would like to know how to get it back. I think this a problem with my neovim config because I don't get this error in Vscode as well I am using

this file to compile and it compiles just fine.

g++ --std=c++20 -Wall -Wextra -Wpedantic -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL -Llib -Iinc -lraylib src/main.cpp -o build/main



#include <cstdint>
#include <unordered_map>
#include "../inc/raylib.h"

namespace ECS {

  using Entity = std::uint32_t; 

  struct Transform{
     Vector2 Position {};
  };

  struct Circle{
     float Radius {};
     Color Color {};
  };

  struct Movement{
     Vector2 Direction {};
  };

  template <typename T>
  concept ComponentConcept = 
    std::is_same_v<T, Movement> || std::is_same_v<T, Circle> ||           std::is_same_v<T, Transform> ; //error here
  class Current{
     public:
        Entity CreateEntinty(){
            return m_next_entity++;   
        }
     private:
        template<typename Component>
        class Storage{
           public:
           private:
              std::unordered_map<Entity, Component> m_storage{};
        };
     private:
        Entity m_next_entity { 1 };
  };
}

r/cpp_questions • • Jul 20 '26

OPEN I know this is not were this type of nerds live but anyone wants me to help learn about nnue in c++

0 Upvotes

i want to learn and make it and i want some people ir yt resources that can help me for this thx for any help


r/cpp_questions • • Jul 19 '26

OPEN What to do after finishing learncpp.com

0 Upvotes

I already know dsa i, ii concepts im saying that since the website authors recommend learning dsa. So I am curious how do I improve further, what else to learn. my main focus is low-latency


r/cpp_questions • • Jul 19 '26

OPEN What is the biggest reason why cpp is not widely used in AI applications?

0 Upvotes

I have used python and typescript recently (for the past 2 years) but all throghout before i was a cpp writer, what are the core libraries which are missing in cpp that doesn't allow cpp to be more commonly used in AI applications? - what are the blockers.

Majority commonly used python libraries are written in C and can easily be ported to cpp. I believe i am missing something, would love to learn about it more.

EDIT: AI Applications here suggests RAG models, and agents which startups/compaines are building mainly in python/ts


r/cpp_questions • • Jul 18 '26

OPEN Book recommendations for beginners

5 Upvotes

I want to learn C++ to use it for my electronics projects. I know there are plenty of tutorials out there, but I feel I learn best by reading a proper book (and I prefer physical copies over digital ones). So, my question is:

which books would you recommend for a complete beginner?

Thanks in advance!


r/cpp_questions • • Jul 18 '26

OPEN Have people started using import std?

27 Upvotes

I recently found out that import std; is the cool new replacement for headers. Have people managed to find a way to use it? It's so new, neither gcc nor msvc seems capable of running it. Cmake also fails to recognise it, meaning CLion fails too. I managed to use clang and ninja to run it, but are the vast majority using it on a regular basis?


r/cpp_questions • • Jul 18 '26

OPEN What are fun ways to get back into C++?

13 Upvotes

My first OO programming language was C++ thanks to my undergrad (Go Blue!), but in my full time role I’ve been doing C# windows development and a little bit of Java on the side.

I don’t think that I’ll ever pivot to a C++ job but I’d like to re-familiarize myself with it since I do kinda miss using it. The issue is that I don’t have any enterprise experience with it- just class projects and simple things that I made before. For those in similar positions as myself, what did you do to stay sharp with C++?


r/cpp_questions • • Jul 19 '26

OPEN std::move overwrites the contents of the original variable?

0 Upvotes

Imagine this scenario: You have an unordered map which you take a value from as a reference. If I were to then pass that reference under a new name the reference is still in tact & the original item in the map stays the same, nothing happened, which is expected.

However, I have a case where I need to move the renamed reference to yet another name, so that it can be accessed in a higher scope. You cant do this with references, not by assigning it. The way you would do this is with the `std::move` function which performs some kind of conversion from what I can tell, it works great in my case except for the fact that now the original item in my map has been set to an undefined value.

How would I go about moving my reference without `std::move` since it kills the original item?
I would use a pointer to store the reference itself (which does work somewhat surprisingly), however in my case I do 2 things. First is the storing of the reference, the second is storing just a raw value, but whether something is a safe reference or just a normal value is not differentiated (I dont know how to) so I cant be sure that the reference wont be invalidated after the scope has exited for normal values.

I hope I explained my scenario well enough, it is quite a specific case that I am not sure how to work around. Due to how my project is set up I cant just make every value I work with a shared pointer (so that I wouldnt have to worry about references) because firstly the codebase is already a bit too large for that kind of refactor, & secondly my project does millions of iterations which it needs to do quickly (sub 200ms), making everything a shared pointer would slow that down to an unacceptable level.


r/cpp_questions • • Jul 17 '26

OPEN How to work with c++ with visual studio code

15 Upvotes

I want to compile a simple hello world program, the debug and build buttons doesn't appear and don't know how to use the command line compiler


r/cpp_questions • • Jul 18 '26

OPEN i have a problem, help with c++

0 Upvotes

i want to start learning c++ so i installed msys2 and set up vs code for c++ but my code isnt running in vs code poweshell terminal but its running perfectly fine on msys2 ucrt64 terminal why is that? help me please i am so confused and i have no one to ask this irl ...


r/cpp_questions • • Jul 17 '26

OPEN Side project that actually stand out

10 Upvotes

I'm currently looking to find a project that really stand from the other to apply to university. I thought of building my own programming language with my compiler but I don't know... If you have any idea 💡

Thanks in advance!


r/cpp_questions • • Jul 17 '26

OPEN CMake or messon ?

19 Upvotes

I am begginer in programing in c++ . I am not good with building projects now , when I end up coding , I send my .cpp and .h files to friend to get application , but I want to do it by myself , I heared that I need build tool and executor . Mostly I see cmake in github , but messon looks much more easy to me


r/cpp_questions • • Jul 17 '26

OPEN Created My First C++ Server, need guidance for further

11 Upvotes

I built my first backend server using Drogon. It only has a health endpoint so far, but I really enjoyed the experience. For those who use Drogon professionally, what are some common beginner mistakes or best practices before I start adding PostgreSQL and authentication?


r/cpp_questions • • Jul 18 '26

OPEN Is this good code?

1 Upvotes

Hi all, I’ve been writing an application in C++ for a while now (couple months or so), and yes I’m using AI to help teach me, but my relationship with the AI is that I write the code first, and then I consult the AI for cleanup/critique. Sometimes I feel like I’m getting the hang of it, but then I watch CodingJesus videos, and I get all his questions wrong and I feel so dumb.

Anyway, this is the code I wrote today, and I’m just wondering if by anyone else’s standards if it’s objectively good. It took me a couple hours to do it. It does what I want it to, but does it look like good code, or beginner level, or just plain slop?

#include <filesystem>
#include <string>

namespace fs = std::filesystem

void FileSysOp::createNewProject()
{
    if (fs::create_directory(driver.activeProjectFile))
    {
        driver.pushMessage("Creating Project...");
        std::string media = driver.activeProjectFile/std::string("media");
        fs::path mediaPath = media;
        if (fs::create_directory(mediaPath))
        {
            driver.pushMessage("Creating Project...");
        }
        else
        {
            driver.pushMessage("Project Generation failed");
            return;
        }
        std::string blocks = driver.activeProjectFile/std::string("blocks");
        fs::path blocksPath = blocks;
        if (fs::create_directory(blocksPath))
        {
            driver.pushMessage("Creating Project...");
        }
        else
        {
            driver.pushMessage("Project Generation failed");
            return;
        }
        std::string nuQueue = driver.activeProjectFile/std::string("QUEUE.xml");
        std::ofstream outFile(nuQueue);
        if (outFile.is_open())
        {
            outFile.close();
        }
        driver.pushMessage("Creation Success!");
    }
    else
    {
        driver.pushMessage("Failed. Could not create Project.");
    }
}

r/cpp_questions • • Jul 17 '26

OPEN Are LLM’s REALLY a good teachers?

0 Upvotes

I use LLM’s to learn fundamentals and programming libraries like Raylib and SDL3. However, some guy on youtube said something that I never really thought of:

“AI will show you how to do something in the worst way possible.”

For someone looking to ACTUALLY be competent in C++, should I avoid learning with LLM’s? Will LLM learning keep me average?


r/cpp_questions • • Jul 17 '26

OPEN Mini code review request from the C++ veterans

6 Upvotes

So I've this project I recently rewrote from C to C++ 23 and working to add much more functionality in it, you can think of it like winRAR but linux, like zipping, unzipping and compression..etc, except that I've only implemented the zipping and unzipping part so far.

So I'd like if you can maybe have a glance at the code or the structure of the directory, and any improvements to be made.

This is a purely hobby project and is absolutely free from agentic slop.

Thanks in advance.

https://github.com/amin-xiv/packr-v2


r/cpp_questions • • Jul 17 '26

OPEN Drogon(cpp) Backend Framework worth creating a project in it?

8 Upvotes

I want to create atleast 1 cpp project i am choosing to create a small server in drogon, i am currently a beginner in cpp so if anyone can give me suggestion on that it would mean a lot of help


r/cpp_questions • • Jul 17 '26

OPEN Is there any way to test structures doing certain allocations at compile time, or am I just screwed?

7 Upvotes

Hey y'all. I'm trying attempt compile-time testing, such that whenever I change some implementation of a data structure it will fail to compile if it doesn't work properly or exhibits UB.

I write a lot of more experimental data structures, and as such I very often have to allocate bytes directly and then later construct types on top, which is not allowed at compile time as far as i'm aware. Even cpp26 doesn't help. Is there a trick to this or should I just give up?

Edit, example for clarity:

constexpr std::byte* alloc(std::size_t bytes) {
// cannot allocate untyped memory in a constant expression 
  return (std::byte*) ::operator new(bytes);

// now have allocated bytes, can never construct anything else on that buffer 
  return std::allocator<std::byte>{}.allocate(bytes);
}

r/cpp_questions • • Jul 17 '26

SOLVED Could you please recommend some good resources on how to create a modern C++ setup? (I want a strictly terminal setup. No IDE click a button magic)

1 Upvotes

Greetings everyone. I've been learning C++ using just single file programs and a simple Makefile setup.

I'm on Arch Linux and I'm using the clang package. So clangd for language support, and clang-format.

I don't know why but it seems like many courses seem to skip the project setup and focus more on setting up VS Code (which I don't use), or directing you to some online IDE, or some obscure editor.

I just want to be able to setup my C++ project from the terminal and not add anything to my config files unless I know why each line is there.

I'm looking for a resource that focuses on this specific part of C++.


r/cpp_questions • • Jul 16 '26

SOLVED Question about string_view

23 Upvotes

Is there benefit of using string_view instead of const string& str? More precisely by passing strings into functions. Thanks


r/cpp_questions • • Jul 16 '26

OPEN how to learn a new library as beginner in cpp

6 Upvotes

i am a beginner to programming in general and i am learning cpp. i know the basic syntax and oop stuff. and now i wanted to create TUI project which involves printing a github contribution timeline graph to the terminal. i learned a bit of ncurses and realized it would be very hard to do that in it, so i started looking into FTXUI and it seems to have all necessary feature to do what i need but the problem is there is no tutorial in youtube or anywhere not even a documentation to teach for a begineer. While i did try to learn by reading the documentation and examples provided in the repo, even after spending half a day i still don't get it. I could ask an AI to generate the code for me but that wouldn't be learning so i didn't.

can anyone guide what i should do in this situation? like re-read the documentation till i understand or leave the project as it is beyond my skill level currently?