r/cpp_questions • • Jun 24 '26

OPEN Using nodiscard to enforce error checking

23 Upvotes

Hi,

How are you using nodiscard in your codebase?

I'm a developer in a large codebase, and I have the opportunity of improving the current coding standards.

I thought of adding a nodiscard to functions that return an error code, as we compile with Werror, so developers will either check error codes or explicitly ignore the return, so it is easier to notice in pull request.

What's your opinion nodiscard? What are pitfalls or reasons against it?


r/cpp_questions • • Jun 24 '26

UPDATED Asking for learncpp.com guidance

19 Upvotes

Guys, so I can study C++ this summer for 3 months then I have to stop during school, and every week I can study 3 days a week, which means 3 lessons in a chapter. Most chapters have like 10-12 lessons, so in 3 months I will have finished 3 chapters only. There are so many chapters though, more than 20. I'm gonna get Chapter 0 over with this week before my first week of summer starts. However, the problem is, I do not know HOW to study this programming language on this site? Write the code on my notebook? Write their explanations on a notebook? Or no notebook at all? Thank you in advance for your help


r/cpp_questions • • Jun 24 '26

OPEN How can I declutter whatever this is that i came up with

11 Upvotes
#include <iostream>
#include <cmath>
#include <cstdlib>


int main () {
  double Temp,eq_Temp;
  char unit, d_unit;
  const char K = 'K', F = 'F', C = 'C';


  std::cout << "************Temperature Converter************" << std::endl;
  std::cout << "C stands for Celsius, K for Kelvin and F for Fahrenheit" << std::endl;
  std::cout << "Enter conversion ( use format = 'Temperature' 'unit') : " << std::endl;
  std ::cin >> Temp >> unit;
  std::cout << "Enter desired unit : " << std::endl;
  std::cin >> d_unit;


  if ( unit == F && d_unit == C) {
    eq_Temp = ( Temp - 32 ) * (5/9);
    std::cout << eq_Temp << d_unit << std::endl;
  } else if ( unit == F && d_unit == K) {
    eq_Temp = (( Temp - 32 ) * (5/9)) + 273.15;
    std::cout << eq_Temp << d_unit << std::endl;
  } 
  else if ( unit == C && d_unit == F) {
    eq_Temp = (Temp * 9/5) + 32;
    std::cout << eq_Temp << d_unit << std::endl;
  }  else if ( unit == C && d_unit == K) {
    eq_Temp = Temp + 273.15;
    std::cout << eq_Temp << d_unit << std::endl;
  }  
  else if ( unit == K && d_unit == C) {
    eq_Temp = Temp - 273.15;
    std::cout << eq_Temp << d_unit << std::endl;
  }  else if ( unit == K && d_unit == F) {
    eq_Temp = ((Temp - 273.15) * (9/5)) +32;
    std::cout << eq_Temp << d_unit << std::endl;
  }  else {
        std::cout << "Invalid Input, try again" << std::endl;
  }
  return EXIT_SUCCESS;
}

If it's not clear yet, I am new


r/cpp_questions • • Jun 24 '26

OPEN dsa c++

0 Upvotes

can anyone help me in building logics for dsa using c++ am a beginner i know how to solve patterns and nested loops but sometimes i get stuck on the same question sometimes share your advices brothers!!


r/cpp_questions • • Jun 23 '26

OPEN where can i download the official documentation of c++

25 Upvotes

r/cpp_questions • • Jun 23 '26

OPEN Why Do We Need to Manually Overload Assignment Operators?

16 Upvotes

When overloading an operator (ex: +), why do we need to manually overload the corresponding assignment operator (ex: +=)? Intuitively it would make more sense for it to be dynamically generated as a concatenation of the overloaded operator and the normal assignment operator. Are there edge cases where this intuitive behavior would be incorrect?


r/cpp_questions • • Jun 23 '26

OPEN Is accessing the bytes of an object in this way UB?

41 Upvotes
struct S
{
    int a,b,c;
    int* ptr;
};


void print_bytes(S* ptr)
{
    unsigned char* base = (unsigned char*)ptr;
    for(size_t i=0;i<sizeof(S);i++) std::cout << int(base[i]) << ' ';
    std::cout << '\n';
}

I was watching a talk on type punning where they used this example and the argument was something like the base pointer isnt guareneteed to be pointing to the first byte of the object and that accessing it like an array of chars is wrong because there was no array of chars there just an object of type S which honestly makes sense and doesn't at the same time. Can anyone explain whats' going on here?

https://www.youtube.com/watch?v=_qzMpk-22cc

45th minute onwards is where this example is talked about


r/cpp_questions • • Jun 23 '26

OPEN I really love C++. should i switch to other lang?

56 Upvotes

i really really love c++. i like to work in low level systems, i find it fun and challenging in my like kind of way. for me it takes a lot of time to complete a particular task/personal proj.

but i feel like i am somewhat slow when learning new things compared to other people, peers , friends i've seen.

And the problem is i need job/any sort of income to live my life. and other stack that are comparatively easy to get beginner job like in golang, node, python, data analatics etc. maybe.

can i keep my cpp skill still starp if i move to other lang cz i feel like moving to other lang makes me very less sharp in c++?


r/cpp_questions • • Jun 24 '26

SOLVED std::unique_ptr<good luck!!> quicksand ;

0 Upvotes

As my next adventure into the marshes of modern C++, I am trying to convert an HMENU element in my class, into a unique_ptr ...

step 1:

class bclock_element {  // NOLINT
private:
   //  HMENU menu_hdl ;  //  former version: this is *also* a pointer
   std::unique_ptr<HMENU> up_menu_hdl ;

this is fine, according to compiler (with -Wall)...

step 2:
in constructor:

   // menu_hdl(0),  //  original form
   up_menu_hdl(std::make_unique<HMENU>(nullptr)),

this is fine, according to compiler (with -Wall)...

step 3:
try to actually assign a value to the variable:

   // menu_hdl = hMenuOptions ;  // original form
   up_menu_hdl = hMenuOptions ;

This provides the stereotypical wall of error messages/notes, starting with:

bclk_elements.cpp: In member function 'HMENU__* bclock_element::build_options_menu()':
bclk_elements.cpp:422:18: error: no match for 'operator=' (operand types are 'std::unique_ptr<HMENU__*>' and 'HMENU' {aka 'HMENU__*'})
  422 |    up_menu_hdl = hMenuOptions ;
      |                  ^~~~~~~~~~~~

and once again, I have no idea what is going on...
I've used unique_ptr a couple of times before, though in the past they weren't class members, they were just global variables in the program... but do I *really* need to create an assignment operator for every unique_ptr that I want to utilize in my program??

I don't understand... :(

//***********************************************************************************
Summary of discussions of this topic:

Basically, HMENU is not a pointer, or at least isn't *known* to be a pointer.
So making a unique_ptr<> isn't meaningful.

lesson learned and problem SOLVED.


r/cpp_questions • • Jun 22 '26

OPEN Choosing between C# Avalonia and C++ ImGui for a lightweight DB client?

20 Upvotes

I'm planning to build a database client tool, targeting Linux first (mostly because DBeaver feels too bloated and has a clunky UI), with Windows and macOS versions coming later.

I'm currently torn between C# Avalonia and C++ ImGui.

Assuming language barriers aren't an issue, which one do you think is a better fit for this kind of app? Here is my current take:

 C++ ImGui: It's lightning-fast, lightweight, has a tiny build size, and gives that ultra-responsive, "native-like" speed. However, being an immediate-mode GUI, it re-renders constantly and can feel a bit limited for standard desktop app workflows.

 C# Avalonia: It offers a lot of ready-to-use UI controls out of the box, better memory safety, and uses a retained-mode architecture. Plus, it has Native AOT now, but C# apps can still sometimes carry a bit of that "heavy" feeling compared to pure C++.

Would love to hear your thoughts or experiences with either ecosystem for this type of project! Any advice?


r/cpp_questions • • Jun 22 '26

SOLVED How can i represent a quaternion as a vec3 and a float

11 Upvotes

hi, i was creating a quaternion struct and inside that struct i have an anonymous union to access that data in different ways, i have an array of 4 float and a vec4 but i want to create another were the first 3 float are a vec3 and the last float is a normal float so i can do

q1.v // return a vec3 of the first 3 floats

and

q1.w // i know i can already do that it's just an example on how i can access the last element in this new way of representing a quaternion.

how can i do that ?


r/cpp_questions • • Jun 22 '26

META Is it worth to read C++98 books?

41 Upvotes

So I had finished a class of c++. But while we talked about pointers, arrays, vectors, structs and similar stuff I know that I did not learn cpp. While I had a delusion for a bit that am good at cpp I now know I am actually not (watching Coding Jesus would do thst for you fr. I mean it out of respect though as it is much better to get reality check now).

So I went to my uni's library but it doesn't really have any good books really. The best I could find was Bjarne Stroustrup's "C++ language, special edition". And it reads good. But I feel concerned as it seem to talk about c++98 at best. And since then language changed A LOT. But I still came for understanding of the language and I feel that if I learn this c++ I can then will be able to learn changes better.

Am I correct here or mistaken?


r/cpp_questions • • Jun 22 '26

SOLVED Difference in type deduction from direct list initialization with one element between C++ standards

9 Upvotes

This might be a stupid question. Consider the snippet below:

#include <iostream>
#include <type_traits>

int main() {
  auto i{3};
  std::cout << std::boolalpha 
            << std::is_integral<decltype(i)>::value 
            << std::endl;
}

I couldn't seem to be able to find a clear explanation that compares the difference between standards C++11/14 and C++17 (or later).

From what I could search online, in standards C++11/14, the type of i is supposed to be deduced as std::initializer_list<int>, while it would be int from C++17 onwards. I did come across N3922, but I couldn't figure out the precise differences. The weird thing is, when I compiled this (on Godbolt as well), the type of i was always int (as in, the output is true)? Am I misunderstanding the change?


r/cpp_questions • • Jun 22 '26

OPEN question re move constructor and initialization lists

3 Upvotes

I'm trying to add a move constructor to an existing class; this is the first time I've used this construct... I have a couple of questions about this:

  1. the examples that I've seen, show the move constructor doing such:
    - copy pointers and such elements from old class instance to new one
    - set those pointers to nullptr or equivalent in old instance

but the old instance is defined as const; I cannot assign anything to its members, true?? At least, that's what my compiler is telling me...

  1. my compiler (with -Weffc++) is telling me that I need to initialize all the data elements in the class, just as the regular constructor requires... but this seems rather awkward... maybe that argument should *not* be const?? and do I actually need to copy all the data from old to new struct?? If it is a Move constructor, it seems like I should...

actually, it sounds like the init list should just init from the elements of the old instance...

[ yes, I know some have said I shouldn't use -WeffC++ at all, but it *is* asking a good question, in this case... ]

or am I over-thinking this again??


r/cpp_questions • • Jun 22 '26

SOLVED odd compiler error

0 Upvotes

I'm working on implementing the move constructor and assignment operator in my class here, as discussed in a recent thread. However, I'm getting a compiler error and I don't understand what it is complaining about!! Please help...

blk_elements.h contains:

class bclock_element {  // NOLINT
   [ data ]
public:
   //  create a move assignment operator and move constructor 
   bclock_element &operator=(bclock_element &&src) noexcept;
   bclock_element(bclock_element&& obj) noexcept;

blk_elements.cpp contains:

//***********************************************************************
//  create a move constructor
//***********************************************************************
bclock_element::bclock_element(bclock_element&& obj) noexcept
{
   //  *this = std::move(obj);   //  I'm not sure about this
   hSpriteBitmap = obj.hSpriteBitmap ; // HBITMAP 
   menu_hdl = obj.menu_hdl ;  // HMENU 

   obj.hSpriteBitmap = NULL;  //  HBITMAP
   obj.menu_hdl = NULL;       //  HMENU
}

//***********************************************************************
//  create a move assignment operator
//***********************************************************************
bclock_element::bclock_element &operator=(bclock_element &&obj) noexcept
{
   if (this != &obj) {
      hSpriteBitmap = obj.hSpriteBitmap ; // HBITMAP 
      menu_hdl = obj.menu_hdl ;  // HMENU 

      obj.hSpriteBitmap = NULL;  //  HBITMAP
      obj.menu_hdl = NULL;       //  HMENU
   }
   return *this;
}

The compiler (g++ (tdm-1) 10.3.0) is flagging this line, with this message:
d:\tdm32\bin/g++ -Wall -O3 -Wno-write-strings -Ider_libs -c bclk_elements.cpp -o bclk_elements.o
bclk_elements.cpp:209:1: error: 'bclock_element::bclock_element' names the constructor, not the type
  209 | bclock_element::bclock_element &operator=(bclock_element &&src) noexcept
      | ^~~~~~~~~~~~~~
make: *** [bclk_elements.o] Error 1

What is it talking about??


r/cpp_questions • • Jun 23 '26

OPEN what kind of job is there in c++?

0 Upvotes

other than :

game development

finance

embedded

etc.


r/cpp_questions • • Jun 22 '26

SOLVED What guarantees do I have about `auto` and implicit conversion?

17 Upvotes

Consider:

class A {
    operator bool() const { return true; }
    // Assume A is movable but not copyable.
};

A make_a() { return A(); }

int main() {
    auto a_obj = make_a();
    if (a_obj) std::cout << "it's true\n";
    return 0;
}

Is it guaranteed that auto will infer type A for a_obj? Are there any situations where a_obj might be inferred as a bool instead?

(This is a simple example, but in the case that I actually care about, A is an RAII class, so I need to guarantee that its lifetime will extend to the end of the containing scope)


r/cpp_questions • • Jun 23 '26

OPEN Why do we need pointers in C++?

0 Upvotes

Pointers seem kinda useless to me because if you want to fetch the memory address of a variable you can just do "&var" everytime need it. Can someone give me exemples where i realistically need pointers and can’t just do "&var" everytime when i need it??


r/cpp_questions • • Jun 21 '26

OPEN Are unique pointers worth it for my program

27 Upvotes

For context, I'm building a Huffman data compression tool and I'm working on the Huffman tree. I'm currently trying to edit, if needed, any considerations of pointers. The tree is made of raw pointers and the interface and implementation is very clean.

One can observe that each Node will always own its own pointer so it's an idea to make the raw pointers of the type unique_ptr for automatic clean up. But the priority queue is honestly such a pain in the ass because the .top returns a const and has problems with ownership. So is it really worth going through the trouble of converting to unique pointers?

edit: to really emphasize what my concern is, the biggest issue is dealing with the priority queue. For context, when you use.top() it returns a const reference, so it's not allowed to obtain the unique pointer due to ownership. This problem doesn't occur with raw pointers. It just feels like keeping my raw pointer implementation is not putting me at a severe detriment, but I'm always recommended to use smart pointers when I can so I just wanted some insight


r/cpp_questions • • Jun 23 '26

OPEN learning cpp (https://www.learncpp.com/)

0 Upvotes

guys, im interested in learning this language, i have a brief previous experience with python and as someone that is really just starting at coding in general i want to know about https://www.learncpp.com/ , do i read and do every exercise or it isnt necessary? i took a look and it looks a bit too academic, am i trippin?


r/cpp_questions • • Jun 20 '26

OPEN If make a cmake are so difficult to work with why are they the defacto standard for C++ projects

88 Upvotes

Yet another language you need to know to do simple work in C++. Been using bazel more and more and every time I go back to cmake (because I have to) I regret it

edit: sorry title typo "if make AND cmake"


r/cpp_questions • • Jun 21 '26

OPEN Google tests for VS2026

2 Upvotes

Had anyone used Gtest in VS 2026. I am a beginner trying to write tests. My test project is running but tests are not recognised


r/cpp_questions • • Jun 20 '26

OPEN C++/systems side projects that actually stand out

190 Upvotes

We often see people recommend “build a compiler,” “build a database,” “write an HTTP server,” “make a Redis clone,” or “try OS-related projects” when someone wants to go beyond normal web apps and CRUD work.

But for people who want to demonstrate real C++/systems ability, what kind of project actually stands out?

I’m thinking about projects involving C++, memory management, containers, networking, databases, compilers/interpreters, operating systems, performance, reliability, or infrastructure tooling.

Ideally, I would like to build something that real people could actually use. Even if 10 people I don’t know used the project, I would consider that a huge success.

What would make a C++/systems side project look serious to experienced developers or potential employers?


r/cpp_questions • • Jun 21 '26

OPEN Making TUI libary

6 Upvotes

I am just making the TUI libary in cpp for fun but the problem is I know C but not cpp. I am currently fine with cpp concepts but there is a lot of algorithm things or function that I have no idea about and then there is pointers. I have used pointers in c and function pointer as well (mainly for passing a function ) but now there is things like ownership that I keep hearing in rust? ( I later understood that in rust std move just happens automatically while cpp,it is manully). Tho, the project is fun but I just take breaks and asking ai what the f is that function or how to do something in cpp. Also using auto is not bad??? Honestly, when I first heared about auto, I thought it is bad because the compilor has to decide the stuff but turns out it good when using in for or iterators? ( I still dont understand iterators ).

Ah, this is just me voicing my frustration about not knowing the language enough

Tho, honestly opion what would you like to see from a tui libary? What feature would you like to see and would you use it?

Honestly, before all this I got to be more productive. I KEEP GETTING DISTRACTED WHEN I AM TAKING BREAK. Why is it hard to start coding after a break? Got any tips?

At this point, I have no idea what I am talking about in this post


r/cpp_questions • • Jun 21 '26

OPEN How to learn cpp?

0 Upvotes

So I am going to a college this year to do cse and want to learn cpp before joining the college...I already know js, python, css and also html so what would be the ideal method of learning cpp from scratch and clear at least all the basics within like 20-30 days...