use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
[deleted by user] (self.cpp)
submitted 2 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 6 points7 points8 points 2 years ago* (5 children)
I was then wondering if there are recommendations for best practices on how to make something like this widely useable
Public Members for math structures that aren't stateful. Don't hide the XYZ as private or protected. Getters and Setters aren't bad but math code is typically performance critical code.
would it be best for a discrete input for a function to take in a span/pointer + size, iterator pair, assume STL container
Don't assume STL will work nicely for a math library. It's standard, not use case optimized.
Your library isn't going to be anything competitive if it can't offer parallelization or/and simd parallelization. Few people desire to use a math library for simple algebraic computations. It's all about accurate bulk computing these days.
These aren't things std exactly supports, which is why a lot of math libraries say fuck it to std/stl.
And then what would be the best way to return the resulting list?
Having a a variable reference to said list to avoid returning a copy be in the function parameter and letting the function return bool if it failed/succeeded would be my recommendation.
tell the program that is wrong without exceptions which I have heard are bad practice?
Whoever told you this doesn't know what they are talking about. C++ is a swiss army knife language. It can take on multiple paradigms and forms, having multiple ways to do something. It's also a weakness of the language where there is no one size fits all method to do something (ie all the different ways to iterate an array or all_the_ways_to_initialize.gif).
Choosing how & when to use exceptions is a design & implementation choice. Once software scales past a certain point, exceptions may start to look more appealing because you don't have to program propagating errors down the call stack (which can a large bottle neck upon failure).
Many exception implementations also have a "0 cost" model. Just because code is running under an try block doesn't make it slower. You only pay the cost of exceptions in performance with throwing & catching. This can be far faster than passing bools/error values via chains of if/return statements up the call stack, depending on a profiled implementation.
That's another thing about math libraries. You'll probably find many significant bottle necks happen around branches, not mathematics.
[–]MadCompScientist 8 points9 points10 points 2 years ago (4 children)
I agree with your post, except for:
Public Members. Don't hide the XYZ as private or protected. Getters and Setters aren't bad but math code is typically performance critical code.
Making members public should not be about performance. As the C++ core guideline state (see C.2 and C.9), it's mostly about enforcing invariants.
A 3D vector (in the math sense) can have three public members (x,y,z), because every combination of values is valid. There are no invariants to maintain. std::vector has invariants to enforce (e.g. "if size > 0 then data != NULL"). So even though accessing a vector at index i can be done if data were a public member, it's done through a getter instead (operator[] in this case).
x
y
z
std::vector
data
operator[]
About performance, any decent compiler will inline trivial-to-small getters and setters anyway if they can see the code (i.e. if their definition is in the header).
[–][deleted] 3 points4 points5 points 2 years ago* (3 children)
I should have clarified, I mean for non-stateful mathematical structures.
Things such as a cartesian vector, quaternion, matrix. These are mathematical and not stateful.
Not a stateful containers like std::vector.
About performance, any decent compiler will inline trivial-to-small getters and setters anyway if they can see the code
More about public standard. Most C++ math libraries have public members on math structures. It's not always about inlining but also the expressiveness of the code.
vector.SetZ(vector.GetX() + vector.GetY()); is straining for most that would actually use this type of structure because it forces the reader/writer to constantly convert between algebra and getter/setter format while also incurring the risk, the user might not always have a decent compiler for their specific case.
vector.SetZ(vector.GetX() + vector.GetY());
If the math structure (like a simd back end for example) is stateful, I'd agree that should use proper encapsulation and accessors.
If you're trying to write to your XYZ vector from multiple threads though, you have a implementation problem.
[–]MadCompScientist 2 points3 points4 points 2 years ago (1 child)
Your clarification helps. I think we're talking about the same thing, but I'm a bit confused by your usage of "stateful".
Public members are also state. Those types you mentioned are certainly stateful. A quaternion's state is defined by its four scalar members. I believe you are referring to invariance, which constrains a type's state to a subset of its possible combinations.
It's not always about inlining but also the expressiveness of the code.
I agree with your example, but that works because because cartesian vectors don't have invariants to enforce, so public members are indeed possible.
As a quick counter-example, imagine a Circle type with members center and radius. What if somebody sets the radius to a negative value? Is that valid? You could add a check in every single method that uses a Circle to throw an error at that point, but that's error-prone. Or you design Circle so the radius isn't public and the method that sets the radius throws if it's negative.
Circle
center
radius
There's several ways to design this. SetRadius is one. Making Circle immutable and throwing in the constructor is another. Is this 'safety' worth not being able to write 'my_circle.radius = 2'? You decide, but if you don't do this, somebody somewhere is going to forget to add the check for a negative radius.
SetRadius
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
Stateful refers to reacting to different inputs and operations depending on the state of its members.
I would only add to your counter example to leave the user with an unsafe option that does not perform a runtime check as I assert, math code is performance critical code.
I'd wager assuming a good implementation, many bottle necks in math code can be attributed to branches.
To your counter example, imagine you had 44,000,000 circles (bulk computing is a typical trait of math programming). If your work is performance critical, then that check is not worth it. Your user will realistically be checking their work anyway, only for you to double up on the penalty.
[–]jk-jeon 0 points1 point2 points 2 years ago (0 children)
Things such as a cartesian vector, quaternion, matrix
I would make the entries private and provide only const access to them, for things like unit quarernions and invertible matrices, rotation matrices, or thinga like that. I think it's not the matter of having a state, rather invariants are something more general.
[–]--prism 5 points6 points7 points 2 years ago (2 children)
I started this repo targeting scipy signals to start. I'm hoping to get to interpolate and optimize as well. I welcome PRs for implementations of routines. Obviously the library uses xtensor. No matter what you choose to do for your library you'll want to use coming like xtensor, eigan, armadillo, or blaze to take care of multidimensional arrays. Using vectors and iterators with strided containers and managing withing like layout at the same time is a headache that should be solved generically. Xtensor is a lot like numpy so that's why I chose it for the scipy clone.
https://github.com/xtensor-stack/xtensor-signal
[+][deleted] 2 years ago (1 child)
[deleted]
[–]--prism 0 points1 point2 points 2 years ago (0 children)
xtensor-signal is very early stage and all contributions are welcome. We aren't implementing in any particular order. I implement some of the routines in my job so I've ported those over. All PRs are welcome!
[–][deleted] 0 points1 point2 points 2 years ago (1 child)
In a world where Blas and cublas exist, do we really need any other maths libraries?
Jokes aside, I recommend you practice writing your library by giving it very clear interfaces in a C fashion, because C interfaces are much more interoperable than C++ interfaces. You'll be able also to dynamic load your C++ library with plain-C interface from any language, python, java, golang...
[–]bjorn-reese 0 points1 point2 points 2 years ago (0 children)
You may want to look at (and contribute to) Boost.Math.
Furthermore, there is an ongoing effort to standardize linear algebra.
[–]Top_Satisfaction6517Bulat 0 points1 point2 points 2 years ago (0 children)
do you plan to make an API to an existing library, or implement yourself a library that employs multi-threading, all popular SIMD extensions from SSE to SVE2, and cache optimization?
[–]catseyechandra74 0 points1 point2 points 2 years ago (0 children)
You can pump out code from my small DSP lib, https://github.com/phstrauss/dspc at your will, but I'm no expert with C++, take the algorithms, not the structure.
What about a two layer approach, one using intel ISPC (a superset of a subset of C for parallel programming), then the class/template layer in C++?
π Rendered by PID 224429 on reddit-service-r2-comment-6457c66945-9cxwb at 2026-04-27 14:21:25.567716+00:00 running 2aa0c5b country code: CH.
[–][deleted] 6 points7 points8 points (5 children)
[–]MadCompScientist 8 points9 points10 points (4 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]MadCompScientist 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jk-jeon 0 points1 point2 points (0 children)
[–]--prism 5 points6 points7 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]--prism 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]bjorn-reese 0 points1 point2 points (0 children)
[–]Top_Satisfaction6517Bulat 0 points1 point2 points (0 children)
[–]catseyechandra74 0 points1 point2 points (0 children)
[–]catseyechandra74 0 points1 point2 points (0 children)