41 questions from the last 7 days
Best practices
8
votes
28
replies
2k
views
Moving from C, how can I implement a doubly linked list in C++ using C++ features?
I'm moving from C to C++ and I'm trying to avoid using C practices in C++.
It is said that raw pointers are a C feature, and that in C++ I should use smart pointers instead, yet every example I have ...
11
votes
5
answers
1k
views
Finding an operator for 3 distinct values with bitwise operations
Consider the set {A,B,C} and an operator op defined by the following table
A op A = A
A op B = A
A op C = A
B op A = A
B op B = B
B op C = C
C op A = A
C op B = C
C op C = B
I would like to encode A,...
Advice
2
votes
11
replies
389
views
I can't understand structure padding in C/C++
I watched a few tutorials on YouTube. They said to assume a 32-bit architecture and then said "a single CPU cycle can only access a word (equals the CPU's native size i.e 4 bytes) at a time from ...
Advice
1
vote
17
replies
195
views
Surprising behaviour of using static_cast on struct with bit fields
The following code compiles on std >= C++20 but fails to compile on std < C++20. This holds for msvc, gcc and clang.
#include <cstdio>
int main()
{
struct byte
{
unsigned ...
Best practices
2
votes
5
replies
151
views
Forwarding C-style struct matrix to constructors of same-size matrix of objects
Suppose I have a C-style matrix of elements
typedef struct _RAW_DATA{
int a;
int b;
}RAW_DATA;
inside a templated class, that defines the size of the matrix:
template<size_t TDim>
...
2
votes
2
answers
154
views
How to avoid implicit conversion in C++ concepts?
Is there a way to prevent implicit conversions happening in concepts? For example, this example compiles with GCC and clang when I actually want it to give an error that the char constructor is ...
1
vote
2
answers
367
views
What impediments are there to inclusion of the Networking TS in the C++26 standard?
As C++26 standard's timeline is getting near, I want to know:
What is(are) the reason for no built in support of networking by the C++ standard. That is, what exactly are the problems that lead to ...
Advice
3
votes
8
replies
132
views
Is IMG_Load from SDL2/SDL_image thread safe
I want to use multiple threads to decode PNG files to speed up loading in my C++ game engine.
The easiest solution is to use IMG_Load from SDL_Image, as I already use SDL, e.g.:
surface = IMG_Load(...
0
votes
2
answers
119
views
Do restrictions on statements in the global module fragment only apply to BEFORE preprocessing and not AFTER?
I think it's common knowledge that you can't have statements in the global module fragment, and that the global module fragment is restricted to "preprocessor" directives only. With:
module;
...
2
votes
1
answer
133
views
Win32 C++ SystemTimeToFileTime, FileTimeToLocalFileTime called after VariantTimeToSystemTime give incorrect results
I am reading the WPD_OBJECT_DATE_MODIFIED time value of a file/folder on a Portable Device (WPD) and need to display it similar to how Explorer does. I have a folder whose Date Modified is displayed ...
2
votes
3
answers
113
views
C++ std::span overload resolution
I want to pass a C-style array to a function taking a std::span, but overload resolution chooses to convert the array to bool instead. Is this behavior required by the standard, or is it a compiler ...
1
vote
2
answers
108
views
Issue with multithreaded/concurrent use of ReadFile() Windows API
I'm currently working on a project that requires a multi-threaded approach. This project is currently platform specific, i.e. Windows.
This is how my read code looks like:
#pragma once
#include <...
2
votes
1
answer
95
views
Is there a way to get the QML engine within a registered C++ QML type object?
If I use a C++ object's property from QML like this:
class Cpp
{
...
Q_PROPERTY(... aProperty READ getAProperty
...
... getAProperty()
{
// which engine called me?..
}
};
// ...
4
votes
1
answer
121
views
Second GLFW window won't render?
I decided to try a dual screen to display the square as a wireframe vs a colored in square:
#include <GLAD/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void ...
4
votes
1
answer
143
views
printf() not working on colab while running a CUDA c++ code
This is my first time working with CUDA programs. So I just wrote a simple hello world program.
#include <stdio.h>
__global__ void hello(){
printf("Hello block: %u and thread: %u\n"...