Questions tagged [c++]

Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.

Filter by
Sorted by
Tagged with
0 votes
1 answer
59 views

Reducing contention/locking in a job system involving read/write dependencies?

I have a job system that has two different types of dependencies per-job: Jobs can rely on other jobs to be completed first, but this is optional Jobs have read/write dependencies (shared/exclusive) ...
-3 votes
1 answer
168 views

What is the use of Multiple Inheritance in languages like C++ and Python?

C++ and Python are the only two languages I know supporting multiple Inheritance. Other object oriented languages I have seen like Java and Ruby do not support multiple inheritance. Even the front-end ...
0 votes
2 answers
89 views

std::string_view and std::string in utility library interface

We have several medium to large software projects targeting different platforms (arm32/64 and x86/amd64 on Linux and Windows) with a lot of duplicated code in them, since every project has its own ...
0 votes
3 answers
109 views

Is it possible to instantiate a template class at runtime?

Suppose I have two abstract classes called Color and Animal And I can create classes Green/Red/Blue derived from Color and classes Dog/Cat/Pig derived from Animal at runtime using factory pattern. ...
0 votes
3 answers
168 views

Optimizing a string to enum converter [closed]

I have built a string to enum converter, which converts a known set of strings that it receives into enum, in order to satisfy a function pointer. Some of you may recognize some elements of this ...
3 votes
5 answers
2k views

Alternatives to if-else on data reading

I have some code on Arduino (so, written in C++) that receives a String through the UART terminal, reads the String, then decides what String to print back and how many things to print depending on ...
1 vote
2 answers
183 views

Is it bad code design to use an out of scope static variable?

I have a function that uses a static variable to count how many times it got called: void func () { static int counter=0; counter++; /* some code */ } The code in the function executes ...
  • 121
-3 votes
1 answer
105 views

What is the importance of "this" pointer in cpp? [closed]

I apologize for asking this intuitive question. I'm not good at programming and my recent work requires me to work a lot with SDEs. I wonder what might be the advantages using the this pointer? I ...
  • 15
2 votes
2 answers
288 views

When should one utilize Atomic Operations?

I have never used atomic operations, and remain largely ignorant of how to utilize them. And yet, I often come across objects like this when peering into Qt's backend: https://doc.qt.io/qt-6/...
  • 3,389
3 votes
1 answer
93 views

const correctness re: reference members

tl;dr is is a good idea for const methods to be able to mutate objects referenced as reference members? Imagine you have some kind of work queue that uses items that look like so: class Consumable { /...
1 vote
2 answers
248 views

In what language does a method not return a value and a function does?

I don't know from where I got this but in my head a function returns a value and a method does not. I know that with OOP a method is a function related to a class. But I am trying to remember where I ...
0 votes
1 answer
76 views

Design of A Multithreaded Simulator

When designing my simulator, I have gotten stuck on 2 main design choices. The simulator can be described as having X number of nodes (between 50 - 2000) that each need to independently do some ...
1 vote
3 answers
179 views

Class design: should I split responsibilities or should the class take care of itself?

I have a Curve class that has some CurveData inside as private member as well as getter functions for topics of interest (e.g. peaks, width and so). So the math logic of a curve is inside this class. ...
0 votes
0 answers
51 views

Avoid double allocations when wrapping a class that requires to be created as a shared_ptr

I'm using mqtt_cpp as a mqtt client in my applications. But because I want to limit the api that is provided by mqtt_cpp and adapt it to my needs I came up with the idea of wrapping this class into my ...
  • 275
0 votes
1 answer
95 views

What information should one infer from a class header that marks its constructor explicit?

If a class is written well, you should be able to gleam all relevant information about the class simply by looking at its header. If one sees that the constructor is marked explicit: What should one ...
  • 3,389
1 vote
3 answers
463 views

Should I use the "die" idiom in C++?

If we implement the following function: template <typename... Ts> [[noreturn]] inline bool die(std::string_view message_format = "", Ts&&... args); We can then write: if (...
  • 2,447
1 vote
1 answer
131 views

C++20, specialize struct once per type and allow member functions to take type as reference, raw, std::unique|shared_ptr or any other smart ptr

Situation I have some existing functions which expect containers which can be iterated and which have certain value_type. The value_type can be a value or some (smart) pointer. All the value_type have ...
1 vote
0 answers
235 views

Best way to store key-value pairs of different types in c++?

I'm reading values from a (PE) binary file. The values have a known length and position in the file and are stored with no padding (right next to each other). They store various types (int, long, ...
  • 111
-3 votes
1 answer
97 views

How would a SRP `class Car` look like? [closed]

SRP insists that a class should have only a single a reason to change. Please guide me through the following example. class Car { drive_forward(); embark(); disembark(); fuel_up(); } ...
  • 6,955
0 votes
1 answer
145 views

is there a two-way (bidirectional) observer pattern?

I have two classes. "System" and "Bluetooth". I want to have a really loose coherency between these two classes as they should be able to exist or not without depending on each ...
0 votes
3 answers
210 views

Use aggregation like LSP in C++

I was reading about LSP (Liskov Substitution Principle) in a book called Clean Architecture: A Craftsman's Guide to Software Structure and Design. I have a question regarding how this would be ...
-1 votes
2 answers
141 views

Performance hit after non-functional refactoring

I was asked to refactor some C++ code recently for the purposes of increasing unit testing coverage. The problem was that the code was tightly coupled on one compilation unit, so we had the equivalent ...
3 votes
1 answer
162 views

C++ Abstract classes vs selective compilation

I'm trying to find a way to achieve abstraction with the lowest possible overhead. This is for an embedded system (Cortex-M4), I use the latest gcc compiler arm-none-eabi. Let's say we have a pure ...
1 vote
1 answer
218 views

In C++, how is signed integer stored?

I'm aware that there are a few accepted implementations of how signed integers are stored, specifically negative values. There's two's complement, one's complement, and sign/magnitude. For Visual C++, ...
  • 33
1 vote
2 answers
168 views

If an object x is an instance of a type T, then what is a type T for a concept C?

In concept-based programming (as in C++ concepts), I am wondering if there is a noun to say that: A type T is an XXXX of a concept C. in the same way we can say that: An object x is an instance of ...
  • 159
4 votes
2 answers
381 views

Testing interaction with proprietary software without a license

First a bit of context: My company develops a software and middlewares to make our software interacts with other software. One of a client has a proprietary software we should interact with, he gave ...
  • 954
1 vote
1 answer
118 views

What is the best way to program a lattice graph?

What is the best way to program a graph like this: I know I could use Adjacency list or Adjacency matrix: //https://stackoverflow.com/questions/5493474/graph-implementation-c //Clearer's answer ...
  • 161
-2 votes
3 answers
215 views

How to signal a dealbreaker error from a c-tor?

Context: 128kB RAM, freeRTOS. Considered solutions: Exceptions. Discouraged by both the memory size and the code style guide. Late bool init(...);. Has worked for a decade but has it's problems - can ...
  • 6,955
0 votes
1 answer
298 views

aliases for namespaces in headers?

Everytime I write a header I end up doing something like this: #ifndef D723E2D5_1943_4166_87CC_73F5C9C47544 #define D723E2D5_1943_4166_87CC_73F5C9C47544 #include "RandomIntegers.hpp" ... #...
  • 161
-1 votes
1 answer
115 views

Smart pointer class choice: Simplicity vs. right tool for the job

I'm wrestling with a design choice question here. I've got a class that needs a couple of semaphores. Semaphores are non-movable objects. Objects of this class however need to go into a vector (there'...
  • 1,049
1 vote
1 answer
133 views

Free standing functions usable for Base pointers

I read multiple times that when a member function only uses API of class it is member of, then it should be made free standing and put in to same namespace, for example, bad practice: namespace A { ...
  • 161
1 vote
2 answers
609 views

Recommended way of hiding implementation details?

I have a single *.h file. This file contains a single (more to come) function declaration. Now the implementation of that file is very complex. the corresponding *.cpp contains several function ...
's user avatar
0 votes
2 answers
122 views

Accessing Hardware-based resources from multiple worker threads

I have a technical application that interacts with different hardware components: measurement devices, sensors, custom hardware. These use dedicated interfaces like USB, serial ports, TCP/IP ...
  • 119
-3 votes
2 answers
180 views

Using templates to define sizes of containers?

Is using templates for specifying capacities of containers a good use or misue of the templates? Eg. template <unsigned int Length> class String { public: String(); String(String& ...
0 votes
1 answer
133 views

std::string violates Single Responsibility Principle? [closed]

I recently attended a C++ talk on SOLID principles and the presenter provided std::string as an example that violates SRP. I wasn't quite able to comprehend why that was the case. From what I ...
  • 199
2 votes
1 answer
371 views

Difference between `Class.X` and `Class.getX()`?

Might be a silly question or something I might have just messed up in my head but here we go... I saw a code example of someone using getPos() in their own class to retrieve the current position of an ...
0 votes
1 answer
147 views

In C and C++, can a compiler store an unamed object generated from a expression, if that same expression is reused, but the result is unchanged? [closed]

I'm mostly interested in C and C++, but I think this question can also apply to other languages. My question is, if an unamed object or value is generated from the evaluation of an expression or ...
1 vote
1 answer
76 views

How to integrate a profiler in the least invasive way possible?

I am currently using tracy to profile a program I am writing. Tracy as a tool seems quite awesome, but it has one issue. You need to markup your code when profiling. This has the negative consequence ...
  • 241
0 votes
2 answers
162 views

Debugging to track attribute change

I have a very large C++ application running on a *nix environment, which is occasionally setting a particular date attribute on a structure, way off what it should be. I know which structure and which ...
2 votes
2 answers
319 views

How to handle duplicate null checks

I'm working with a legacy codebase that has a lot of functions with a nullcheck on the same object. Example: std::vector<SessionNode*> * SessionManager::SessionMap; Session * getSession(int ...
1 vote
0 answers
53 views

Create Views depending on the dynamic type of an object returned by the model in MVP

In my MVP app, my model (a service) returns a collection of ISession pointers. Each pointer points to an object of a derived type HttpSession or HttpsSession. The model (the service) knows nothing ...
  • 111
-1 votes
1 answer
44 views

Create new object based upon another abstract object

Assume the following class structure: class Base; class A : public Base; class B : public Base; class WrapperBase; class AWrapper : public WrapperBase; class BWrapper : public WrapperBase; I ...
2 votes
2 answers
197 views

When calculating the runtime of a c++ program, how do you factor in constexpr/tmp functions?

It's mostly all in the title. How do you report the runtime of a C++ program that has intensive calculations at compile-time? If I have a program that, when I use the bash time command on it, appears ...
0 votes
1 answer
331 views

Why is there no Hashmap in C++ like Java? [closed]

I know that the C++ standard library includes the ordered and the unordered map (std::map and std::unordered_map) containers, but why is there no hashmap available on C++? I was asked this questions ...
3 votes
2 answers
211 views

File scope static variables with non-trivial destructors

I am coming from the Google C++ guidelines, specifically the section on Static and Global variables. It advises against having global variables of types that aren't trivially destructible. I'm unsure ...
  • 199
2 votes
2 answers
178 views

Design pattern to create a shared lib architecture

I am making a renderer as a hobby, one thing I thought to try is making the low levelAPI be dynamically swappable, i.e. you could have an opengl or vulkan backend and switch between the two without ...
  • 241
2 votes
1 answer
147 views

Wrapping a legacy C++ codebase with a C API

I have inherited a large C++ codebase implementing various Windows desktop applications, services and libraries using Windows MFC. There are no automated tests. We need to decouple the UI and retain a ...
  • 91
1 vote
3 answers
657 views

Mocking clock in C++ unit test

I'm working on a project that does things depend on how much time has elapsed. Specifically for testing purposes, I need to call a method twice a second. The basic idea of the test is something like ...
  • 313
0 votes
2 answers
133 views

On hanging Desktop-applications

I am filled with questions pertaining to the situations where a desktop-application hangs, possibly for minutes. I've got this complicated game-engine written in c++. Several times during development, ...
1 vote
1 answer
92 views

Constructor initializer list too bloated?

I noticed that my Member initializer list is not really readable. My main.cpp files looks like this: #include "Application.hpp" using Pathfinding::Core::Application; int main() { ...
  • 161

1
2 3 4 5
53