Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
2,666
questions
1
vote
1
answer
269
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++, ...
1
vote
2
answers
169
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 ...
4
votes
2
answers
390
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 ...
1
vote
1
answer
175
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
...
-2
votes
3
answers
219
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 ...
0
votes
1
answer
635
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"
...
#...
-1
votes
1
answer
117
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
vote
1
answer
135
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
{
...
1
vote
2
answers
1k
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 ...
0
votes
2
answers
155
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 ...
-3
votes
2
answers
265
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
156
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 ...
2
votes
1
answer
374
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
152
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
101
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 ...
0
votes
2
answers
196
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
328
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
60
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 ...
-1
votes
1
answer
45
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
229
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
496
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
334
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 ...
2
votes
2
answers
294
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 ...
2
votes
1
answer
202
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 ...
1
vote
3
answers
1k
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 ...
0
votes
2
answers
178
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
105
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()
{
...
2
votes
1
answer
134
views
Delegating whole class bad practice?
Suppose I have the following struct:
struct Dimension
{
void setDimension(Vector2i dim);
Vector2i getDimension() const;
bool canShowInformation();
}
And this struct uses the Dimension ...
0
votes
4
answers
114
views
API design: Should a count() of a set of consecutively-id'ed elements return a size type?
I'm writing C++ code, where the standard library has an idiomatic type for representing sizes: std::size_t.
Now, I'm writing a function which counts certain kinds of objects; and these objects have ...
-1
votes
3
answers
418
views
What is the difference between Facade and God?
A Facade is a higher level API over a whole subsystem. A God is class that violates SRP. Where do we draw the line? Does the following code represent any of the two:
enum UnitType{Demon, Paladin};
...
-3
votes
1
answer
51
views
Is it possible to add a unique identifier to the shared Data via bittorrent (P2P)?
I know that P2P sharing is copying the exact content between the peers. Everyone has the same data.
Let's assume the content is very important and I don't want anyone to distribute outside the group. ...
0
votes
0
answers
41
views
How could i reduce compilation time without using less templates? [duplicate]
I am part of a small project that is doing some research code in C++.
Our work involves a lot of mathematics and due to its nature we often need to temporarily use other people's code/libraries. To ...
1
vote
2
answers
580
views
Unit testing of classes with functions as parameters in C++
Let's say I have a function in a class with the following signature:
int fun(int x, int y,std::function<int(int, int)> funArg)
The output depends on the operations done in funArg.
My question ...
0
votes
1
answer
87
views
C++ class design with shared pointers methods [closed]
I dont exactly know, how to handle the problem, where one class uses shared pointers but I want to call its method from inside other class via this pointer.
class Bar {
public:
Bar() = default;
~...
0
votes
1
answer
88
views
Plugin system design core component access: Common practice?
I wonder how to design a good plugin architecture in C++/Qt. The main concern I have is about dependency injection vs globals when it comes to the core components. Plugins should have access to ...
1
vote
5
answers
426
views
The size of a byte in platform independent software
When writing platform independent software in C++, can I nowadays safely1 assume that a byte has a size of 8 bits? Or do I have to calculate it like this, for example:
#include <climits>
std::...
5
votes
2
answers
270
views
Should I use classes instead of functions with a state needed for computation?
I have implemented the cows and bulls game in C++.
The code:
#include <cstdio>
#include <cstdlib>
#include <ctime>
struct DigitMatches {
int matches_in_right_positions;
int ...
0
votes
1
answer
182
views
Wrapping the UI framework
I want to write an application where I would delegate certain functionalities to 3rd party libraries. To make sure the code remains modular, I want to put these libraries behind an interface so I can ...
2
votes
2
answers
244
views
Mixing compile time condition with runtime conditions. Bad design?
On a C++ project I got an idea to mix some compile time macro with std::optional to do something like :
#include <optional>
struct Foobar
{
std::optional<int> foo;
std::optional&...
0
votes
1
answer
234
views
Are there reasons why I shouldn't put a cpp file in my interface?
It is quite common to separate C++ code into cpp and hpp files and to define the hpp files as the interface of the code. Therefore, when you use the code as a library, you can expose the interface, ...
6
votes
1
answer
384
views
Specify that a method needs a mutex held when calling
Current situation
Right now I have a method like
Data lookupData(Key id)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_dict.find(id);
if(it == m_dict.end())
{
...
2
votes
1
answer
95
views
Design for deduplicating concurrent tasks in flight simultaneously
I have 1-20 agents that issue requests over time with roughly ~50 in total in flight at any given time. Many of these requests are identical. The tasks are idempotent, but if two identical requests ...
0
votes
1
answer
723
views
Run lambda inside a separate thread with a member variable caught by reference
So, I have the following two functions in THE SAME class:
void start()
{
...
m_pRunnable = createRunnable(
[spDiskManager = m_spDiskManager,
path = m_path,
&success =...
1
vote
1
answer
116
views
Unit testing, testing internals
I have a class, which uses a timer, to do some time-dependent things. Now, I do create the timer inside the class:
class MyClass
{
public:
Myclass(arg1, arg2, ...)
:m_timer(new Timer(delay))
...
...
27
votes
9
answers
8k
views
Is it a security vulnerability to declare class members as public?
I have always wondered whether public, protected, and private has security implications post compilation.
Hypothetically:
class Foo
{
public:
int m_Foo; // Completely vulnerable and dangerous
...
1
vote
2
answers
390
views
Appropriate use for the visitor design pattern
Assume I have a factory which takes in a series of bytes and outputs a pointer to a newly-created abstract message.
Now, I would like to define some extensible, manageable, and clean way to "...
-1
votes
1
answer
159
views
Conversion methods: from_xxx() or to_xxx(), is there a reason I shouldn't stick with to_xxx()?
I have a C++ library that I'm converting to Python. In the C++ library I have multiple constructors and many different types of conversion functions (think radians to degrees, and different types of ...
5
votes
2
answers
770
views
Why would you use 'new' and 'delete' for something that will be referenced through a vector?
I'm going through some code from this article about ECS-systems in game programming and trying to understand it, and something I'm seeing a lot is using heap memory in places where it seems like ...
0
votes
1
answer
86
views
Best Architecture to integrate a camera to a system [closed]
I'm trying to integrate a Camera to a more complex system in C++.
The camera I'm using has a low-level API so I'm thinking about using a more simple interface.
Basically what I'm planning to do is (in ...
0
votes
1
answer
194
views
Documenting classes with doxygen (I want a birds eye view of the code but also want documentation)
When I code in plain C there is quite a beautiful interaction between headers and c files in terms of documenting the code.
I tend to add the documentation on the cc files (and yes I am aware that ...