r_cpp | Unsorted

Telegram-канал r_cpp - C++ - Reddit

-

Stay up-to-date with everything C++! Content directly fetched from the subreddit just for you. Join our group for discussions : @programminginc Powered by : @r_channels

Subscribe to a channel

C++ - Reddit

Does anyone know how to suppress the Raw View in debugging in Visual Studio?



https://redd.it/1b4vswz
@r_cpp

Читать полностью…

C++ - Reddit

From CLI to GUI | Best tips for beginners ?

Hi all,

What would be your best advice for a cpp beginner looking to get started with GUI integration ? I don't really know where to start, I have a hard time pinpointing the important informations :/

​

Thank you and have a wonderful day !

https://redd.it/1b4r6bj
@r_cpp

Читать полностью…

C++ - Reddit

Feel Lost

I started my bachelor's in CS 2 weeks ago at the top of my class rn even though I'm just barely familiar with the C++ basics like arrays, 2D arrays, functions, recursion and stuff but I don't know what to learn next, or what projects should I make to learn more and implement the skills I learn I'm learning pointers rn and learning data structures but honestly don't know what to do with these skills even if I did learn them what am I supposed to do? What should I make? I feel lost jumping from one tutorial series to another just as I become even slightly dissatisfied with the pace at which they are going or their style I haven't even completed a single course.

https://redd.it/1b4q6gf
@r_cpp

Читать полностью…

C++ - Reddit

Is design patterns not much used in C++ coding? I ask this cause most of the resources I find on this topic especially video tutorial are in Java



https://redd.it/1b4imd1
@r_cpp

Читать полностью…

C++ - Reddit

Reinvented the Wheel (On Accident)


I recently embarked on a detailed and passionate project, dedicating a significant amount of time to developing a class that converts 32-bit/64-bit integers into a 32/64-element boolean array. My approach was comprehensive, covering all bases—I even enhanced its functionality by overriding the [\] operator to allow direct element access. This project was a source of immense satisfaction and pride, a feeling I seldom experienced in my work. That was until some asshole decided to inform me that the STL's vector<bool> class already employs a similar technique, leveraging integers and bit manipulation internally. What a buzz kill.
The moral of this story is that investing time to understand the implementation details of the STL classes can be incredibly enlightening and prevent unnecessary work in the future.

https://redd.it/1b471e2
@r_cpp

Читать полностью…

C++ - Reddit

BLAS Algorithm for Computing Inverse

I am trying to understand what algorithm is used by BLAS to compute the inverse of a matrix. When going through the numpy linear algebra documentation for computing the inverse, I saw that it calls a cpp backend. I don't know the language, so couldn't follow. I'm hoping some members of this community will be able to help. Key questions I want to know are :

1. Does it compute the inverse with some efficient algorithm, or "solve" AX=I for the inverse?
2. What is the complexity of the solution?

https://redd.it/1b44d4b
@r_cpp

Читать полностью…

C++ - Reddit

Learning how to use Class in c++ but I don't fully understand the need for setters/getters.

//constructor with arguments
customerData(double price, int select){
total_price = price;
choice = select;
}
//setter functions
void setPrice(double price){
total_price = price;
}
void setChoice(double select){
choice = select;
}
//getter functions
double getPrice(){
return total_price;
}
int getChoice(){
return choice;
}

so I believe I understand the part that has constructor with arguments, we do this to reference our private/protected members of the class to be accessible publicly.

However, I do not understand why we use setters and getters. The setters, especially, seem very redundant since we are essentially repeating what we did in the constructor with arguments? Can someone help me understand a bit more regarding this concept? thank you so much

edit: by the way i do want to add that this is the way we have been learning from my course in object oriented programming.

https://redd.it/1b40dz8
@r_cpp

Читать полностью…

C++ - Reddit

Contracts MVP shipped!

The WG21 Study Group for Contracts announced they have shipped a MVP to the other review study groups. There's still a long way to go, but the longest journey begins with a single step. Next steps will be criticism and review by the Language and Library Evolution groups.
https://wg21.link/p2900r6


>Abstract: In this paper, we propose a Contracts facility for C++ that has been carefully considered by SG21with the highest bar possible for consensus. The proposal includes syntax for specifying three kinds of contract assertions — precondition assertions, postcondition assertions, and assertion statements. In addition, we specify the range of runtime semantics these assertions may have – ignoring, observing, or enforcing the truth of their predicates — as well as the ability to specify a user-defined handler for contract violations. The features proposed in this paper allow C++ users to leverage contract assertions in their ecosystems in numerous ways.


&#x200B;

https://redd.it/1b3xxbs
@r_cpp

Читать полностью…

C++ - Reddit

please explain IMGUI to an old punk.

Hello friends,

Quick background: I have been programming for a long time in low-level C/C++ projects but never in my life did I had to write any sort of GUI, until now.

During my research of how to go about it, the library Dear IMGUI kept popping up as a suggestion, and so I had a look at it - cloned it and run through the demo. It looks great, no doubt, but there are things that I just don't understand and I was wondering if any of you could clarify a few questions.

Suppose that my program is receiving data from a socket and and it is updating the values in a struct of arrays (i.e. think about a matrix or a dataframe) with the data received from the socket.
I would like to visualize all those arrays in a IMGUI "table", but how would that work?

When would the "rendering" happen?

If I assume that one thread is reading from the socket and updating the table, and the other thread is rendering the GUI, then how could the GUI render while the other thread is changing the data underneath without getting into some inconsistent state?


An option, I guess, would be to "trigger" the rendering itself everytime that there's a "mouse move" or that the table data is updated, but if the former, the problem still exists, the table would be rendered with values that could be corrupt if the values in the arrays were not atomic or some mutex was locking the data.

But if a mutex is locking the data and the rendering thread is checking this mutex all the time in a loop, then I assume quite a performance drop?

In summary, how can the rendering happening when other threads are updating the values that are being rendered while being in a consistent state?

Another question that is bugging me is how does "text input" work.
Say that there's a text box and I write something there, would the next rendering frame only happen after I "submit" this text? or is it happening continously? And if the latter how it is keeping track of what I wrote?

Sorry if these seem like trivial questions but I really can't understand this paradigm of interaction between backend and UI, any resources or explanations you can offer would be greatly appreciated as I am sure I am confusing some parts of this.

Thank you. Awesome day to everybody.

https://redd.it/1b3tc4y
@r_cpp

Читать полностью…

C++ - Reddit

How did good ol' copy constructor screw you over, and how are you today?

This has happened to me at least two times I can remember. You make a class with some crap in it. Code looks good. You run the app, start testing it.

Everything goes to shit, possibly with some weird pattern you can't figure out. You waste anywhere from 1-3 hours. Turns out the library you're using this class with is moving things around in memory every so often, something you didn't suspect, and some of your data members are, in fact, sensitive to being copied (possibly a non-owning pointer), corrupting what object has what. So you sprinkle a little move constructor and everything's happy.

I'm fine, but mad I wasted a few hours when I should have known better.

Anyone else? And how are you today?

https://redd.it/1b3jgwi
@r_cpp

Читать полностью…

C++ - Reddit

Introducing hyperion::mpl, C++20 metaprogramming library

(No?) Thanks to layoffs, over the past few weeks I've had a lot of time to break up, clean up, and otherwise improve on a long-term project of mine that I had to put on the backburner for the past year. This is one of the first results of that.

https://github.com/braxtons12/hyperion\_mpl

I'll try to avoid repeating the README too much here. hyperion::mpl is a C++20 metaprogramming library focused on a value-oriented approach. It tries to make metaprogramming (comparatively) simple and easy, and much more like writing "normal" C++ code than the traditional (often recursive) class template based approaches.

Example:

#include <hyperion/mpl.h>

using hyperion::mpl::MetaType;
using hyperion::mpl::List;
using hyperion::mpl::Pair;
using hyperion::mpl::isconst;

constexpr auto as
const = (MetaType auto type) {
return type.asconst();
};

constexpr auto zipped = List<int, double, float>{}
.zip(List<size
t, void, ptrdiff_t>{});
static_assert(zipped
== List<Pair<int, size_t>,
Pair<double, void
>,
Pair<float, ptrdifft>>{});
static
assert(zipped.apply(asconst)
.all
of(isconst));

I know the immediate question you have is "What differentiates this or is better about it vs something like Boost.Hana?"

I think the main selling point of this vs Boost.Hana boils down to the fact that this is simpler. Boost.Hana tries to blur the lines between compile-time and run-time programming, whereas this is focused solely on making the compile-time side of things easier. This is a tradeoff: with Boost.Hana you can do things like the below (pulled from Boost.Hana's README), while with hyperion::mpl, you can't:

auto animals = hana::make
tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
auto names = hana::transform(animals, (auto a) {
return a.name;
});
assert(hana::reverse(names) == hana::maketuple("Snoopy", "Garfield", "Nemo"));
static
assert(hana::length(animals) == 3u);

However, there is a lot of machinery in Boost.Hana to make functionality like that possible, which which can often mean longer compiler times, and, when used incorrectly, can lead to verbose, hard to understand compiler errors. The implementation of hyperion::mpl is much simpler because it sticks strictly to improving compile-time use-cases, and thanks to that simpler implementation, along with usage of C++20 concepts, any compiler errors you do get are generally friendlier as well.

Currently tested/supported compilers/configurations:

Linux: GCC 13, Clang 16

MacOS: Clang 15, Clang 16

Windows: MSVC 19.39

https://redd.it/1b3ad9c
@r_cpp

Читать полностью…

C++ - Reddit

Can someone define for me in one sentence what safe c++ is?

Can someone define for me in one sentence what safe c++ is?

https://redd.it/1b36chb
@r_cpp

Читать полностью…

C++ - Reddit

C++ ( cout<<" Hello "; )

how can i translate the text in the cout after the run.
I mean when it's in the cmd or terminal, i want to be in other languages.

https://redd.it/1b325h3
@r_cpp

Читать полностью…

C++ - Reddit

Is it possible to build a network packet sniffer using c++?

So I was looking out for a project idea as I had recently learnt c++ and was looking out for few project ideas and chatgpt suggest "Network packet Sniffer" as one such idea. I've used wireshark in the past so I know its going to be a greatly challenging project to take on at my current skill level. But I'm still curious as to how it can be done.


https://redd.it/1b2xjt4
@r_cpp

Читать полностью…

C++ - Reddit

How to fix this error

I am using dev c++ and I was trying to insert a string into vector using emplaceback()

But it shows vector has no member named 'emplace
back'



https://redd.it/1b2u2qt
@r_cpp

Читать полностью…

C++ - Reddit

How to write a program for matrix multiplication in C++ with inline assembly?

I have this code for square matrix multiplication of size n in C++ by using inline assembly.

int main() {
    int n;
    cout << "Enter the matrix size:" << endl;
    cin >> n;
    int A[100][100];
    int B[100][100];
    int C[100][100];
    cout << "Enter the elements of the first array:" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
        }
    }
    cout << "Enter the elements of the second array:" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> B[i][j];
        }
    }
    _asm {
        mov edi, A
        mov esi, B
        mov edx, C
        mov ecx, n
        xor ebx, ebx
        outer_loop:
            push ecx
            xor ecx, ecx
            inner_loop:
                push ecx
                xor eax, eax
                mov esi, B
                mov ecx, n
                innermost_loop:
                    push ecx
                    mov eax, dword ptr [edi + ebx * 4]
                    imul eax, dword ptr [esi + ebx * 4]
                    add dword ptr [edx + ebx * 4], eax
                    add esi, 4
                    loop innermost_loop
                    pop ecx
                    add ebx, 1
                    cmp ebx, n
                    jl inner_loop
                    pop ecx
                    add ebx, 1
                    cmp ebx, n
                    jl outer_loop
    }
    cout << "Elements of the resulting matrix:" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << C[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


But it gives a runtime error in line
mov eax, dword ptr [edi + ebx * 4]

The error is: Exception thrown at 0x00232817 in MatrixMultiplicationWithASM.exe: 0xC0000005: Access violation reading location 0x00000001. occurred

I have attempted numerous solutions, however, none have been successful.

https://redd.it/1b4w6i8
@r_cpp

Читать полностью…

C++ - Reddit

Need to branch out from console app graphics!

I've already learned a fair bit of c++, and have been using the console app to draw simple graphics for stuff like snake and a falling sands game. so right now I need a beginner friendly way to draw graphics on the screen. I tried to learn openGL but got completely overwhelmed by the amount of stuff I had to learn to even draw a simple triangle. is there a simpler way to go about drawing pixels on a screen for c++? im imagining a 2d array of rgb values that I pass into a function which draws it to a window. currently, the closest (and simplest) thing that I know which does this is the windows drawpixel which is insanely slow.

https://redd.it/1b4rjoh
@r_cpp

Читать полностью…

C++ - Reddit

Searching for a practical oriented online C++ Course for beginners

Hey guys,

I’m new into C++. I have some basic knowledge about C. I’m searching for an practical oriented online C++ course. Ideally with a mentor ( tutor ).

Do you have any recommendations?

Thanks in advance!

https://redd.it/1b4mv34
@r_cpp

Читать полностью…

C++ - Reddit

Does c++ version affects minimum windows version executable can run on?

For example, will c++11 executable support older windows version compared to c++20? And what is minimum windows version c++20 executable will run or? Or version of c++ doesn't matter and c++20 executable with only void main(){} will run even on windows 95, and what matters is how much of windows API you use?

https://redd.it/1b4gt13
@r_cpp

Читать полностью…

C++ - Reddit

Which language is preferred by companies for technical rounds?

I am very comfortable in c++ as i use it with Qt on day to day basis since my 1st year of undergrad, but now in 3rd year as i want to start appearing for interviews for jobs for on campus placement, i have strong knowledge of data structures concepts, but that is limited to theoretical only, i have never coded competitive programming questions at all, solved the logic for data structures & algorithms subject in college but didn't code.
The reason for this was that i do quantum computing from the start using python and like designing with coding, so i have developed multiple desktop apps using Qt creator and designer, and due to quantum computing i have strong hold on python too.
So i know both c++ and python, but haven't solved data structures problems in any of them. Now i wanna know that which would be easier to do data structures just for clearing tech rounds and interviews, i don't need them any further as i wanna be in quantum computing.
C++ seems harder one obviously but it is better. So can anyone tell me like in interview they focus on logic and problem solving skills or demand coding to be done specifically in c++ as in python libraries are already built.
Please tell what should i prefer in my situation [NOTE : I won't be appearing for software developer engineer roles, but every role has tech round basically even though it is front end developer.\]

https://redd.it/1b3xk3k
@r_cpp

Читать полностью…

C++ - Reddit

Can anyone lay out the necessary parts of the bankers algorithm for me?

Basically for an assignment I have to write the bankers algorithm. Resources online have different info so I'm not sure what is needed and what is not. Is there a good guide online laying out the exact functions, variables, and data structures needed to complete the algorithm? I get the gist of what it does and how it does it, so I'm fairly confident if I had the parts I could make them go together.


Does anyone have any good resources on putting thsi algorithm together? Otherwise if you could lay out the exact functions, data structures, and variables needed that would be great as well.

https://redd.it/1b423fa
@r_cpp

Читать полностью…

C++ - Reddit

Consteval lambda with capture of a constexpr variable. Is it legal?

I recently wrote some code which worked fine for me in MSVC, but when I went to compile with Clang it raised compile errors. To me it seems like this code should be fine and that Clang is wrong to reject it but I wanted to see if someone more knowledgeable than myself knows better. The issue occurs because the lambda is capturing a variable. Even though that variable is constexpr Clang thinks that the lambda can no longer be considered consteval.

Example here: https://godbolt.org/z/1xG1G9qc9

https://redd.it/1b3y6zs
@r_cpp

Читать полностью…

C++ - Reddit

Running make gives "multiple definition of ... first defined here" error

I have following project structure:
```
ProjectX
├── Module1
│ ├── Module2
│ │ ├── CMakeLists.txt
│ │ ├── src
│ │ │ └── Module2.cpp // has #include "Common.h"
│ │ └── include
│ ├── CMakeLists.txt
│ ├── src
│ │ └── Class1.cpp // has #include "Common.h"
│ │ └── Class2.cpp // has #include "Common.h"
│ └── include
│ └── Class1.h // has #include "Class2.h" ** NOTE **
├── Module3
│ ├── CMakeLists.txt
│ ├── src
│ │ └── Class3.cpp // has #include "Common.h"
│ └── include
├── src
│ └── NewClass.cc // newly added
├── include
│ ├── NewClass.h // newly added
│ └── Common.h // newly added
└── CMakeLists.txt
```

`Commonn.h` contains instances of `NewClass` to be used by all Modules:

#ifndef GLOBALS_H
#define GLOBALS_H

#include "NewClass.h"

namespace ProjectX {
extern NewClass instance1 = NewClass();
extern NewClass instance2 = NewClass();
extern NewClass instance2 = NewClass();
}

#endif // GLOBALS_H

I have following CMakeFiles contents:

**ProjectX/CMakeFiles.txt**

add_library(${PROJECT_NAME} SHARED
src/NewClass.cc
include/NewClass.h
include/Common.h
)


**ProjectX/Module1/CMakeFiles.txt** and
**ProjectX/Module2/CMakeFiles.txt**

set(PROJECTX_DIR ${PROJECT_SOURCE_DIR}/../)
target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECTX_DIR}/include/
)

**ProjectX/Module3/CMakeFiles.txt**

set(PROJECTX_DIR ${PROJECT_SOURCE_DIR}/../../)
target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECTX_DIR}/include/
)

Also note five `#include`s specified in the ASCII hierarchy at the top.

I am getting following errors while running `make`:

/usr/bin/ld: CMakeFiles/Module1.dir/src/Class1.cpp.o:(.bss+0x10): multiple definition of `ProjectX::instance1'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x10): first defined here
/usr/bin/ld: CMakeFiles/Module1.dir/src/Class1.cpp.o:(.bss+0x20): multiple definition of `ProjectX::instance2'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x20): first defined here
/usr/bin/ld: CMakeFiles/Module1.dir/src/Class1.cpp.o:(.bss+0x0): multiple definition of `ProjectX::instance3'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x0): first defined here
/usr/bin/ld: Module2/libModule2.a(Module2.cpp.o):(.bss+0x20): multiple definition of `ProjectX::instance2'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x20): first defined here
/usr/bin/ld: Module2/libModule2.a(Module2.cpp.o):(.bss+0x0): multiple definition of `ProjectX::instance3'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x0): first defined here
/usr/bin/ld: Module2/libModule2.a(Module2.cpp.o):(.bss+0x10): multiple definition of `ProjectX::instance1'; CMakeFiles/Module1.dir/src/Class2.cpp.o:(.bss+0x10): first defined here
collect2: error: ld returned 1 exit status

Before including `NewClass.cpp`, `NewClass.h`, and `Common.h`, it was all working just fine. I wanted to add functionality that can be used as a kind of utility across all modules of ProjectX. Thats why I did then code and CMakeLists changes as explained above. But it started to give me above errors. How do I fix this? Also is there any better way to do this?

https://redd.it/1b3u7a9
@r_cpp

Читать полностью…

C++ - Reddit

C++ Show and Tell - March 2024

Use this thread to share anything you've written in C++. This includes:

* a tool you've written
* a game you've been working on
* your first non-trivial C++ program

The rules of this thread are very straight forward:

* The project must involve C++ in some way.
* It must be something you (alone or with others) have done.
* Please share a link, if applicable.
* Please post images, if applicable.

If you're working on a C++ library, you can also share new releases or major updates in a dedicated post as before. The line we're drawing is between "written in C++" and "useful for C++ programmers specifically". If you're writing a C++ library or tool for C++ developers, that's something C++ programmers can use and is on-topic for a main submission. It's different if you're just using C++ to implement a generic program that isn't specifically about C++: you're free to share it here, but it wouldn't quite fit as a standalone post.

Last month's thread: https://www.reddit.com/r/cpp/comments/1agbyt7/c_show_and_tell_february_2024/

https://redd.it/1b3pj0g
@r_cpp

Читать полностью…

C++ - Reddit

hikotest lightweight unit-test framework.
https://github.com/hikogui/hikotest

https://redd.it/1b3hl1z
@r_cpp

Читать полностью…

C++ - Reddit

Light-weight, header-only C++ Timer-alarm library

This is a light-weight, header-only C++ Timer-alarm library that's based on threads as opposed to the POSIX' signal based timers. Therefore, it is a lot less disruptive.

&#x200B;

https://redd.it/1b395st
@r_cpp

Читать полностью…

C++ - Reddit

Looking for website that has list of cpp tips/optimizations

found a cool website a while back and lost the link. It listed 30 or so cpp tips / optimizations (stuff like how you shouldn't use unsigned char for an uint8). Does anyone know this site? I know im shooting in the dark here


also, I found this website that said "Jump statements such as continue, goto, break should always be avoided. These calls are computationally expensive and there are always alternatives. Switch case should never be used if performance is a consideration.". This is totally wrong, beause switch-cases compile into jump-tables, right? So much bad info online. thanks

https://redd.it/1b352tx
@r_cpp

Читать полностью…

C++ - Reddit

Libfork v3: portable continuation stealing with coroutines and cactus stacks
https://github.com/ConorWilliams/libfork

https://redd.it/1b30zcc
@r_cpp

Читать полностью…

C++ - Reddit

A story of a very large loop with a long instruction dependency chain - Johnny's Software Lab
https://johnnysswlab.com/a-story-of-a-very-large-loop-with-a-long-instruction-dependency-chain

https://redd.it/1b2ww4d
@r_cpp

Читать полностью…

C++ - Reddit

Which one should I use -Libfuzzer or Google Fuzztest?

I am trying to develop a deep learning library from scratch (in C,C++,,mostly c++) and I would like to know which of these tools should I use for fuzz testing- Libfuzzer or Google Fuzztest? I'm currently using clang compiler and building with bazel.

https://redd.it/1b2t1xk
@r_cpp

Читать полностью…
Subscribe to a channel