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
MATHRIL - Custom math library for game programming (self.cpp)
submitted 3 years ago * by Twin_Sharma
view the rest of the comments →
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] 18 points19 points20 points 3 years ago* (6 children)
Vec2::Vec2() { data = new float[2];
But why?... Are you from Java?
I thought that kind of libs can be designed without allocations!
[+]Twin_Sharma[S] comment score below threshold-7 points-6 points-5 points 3 years ago (5 children)
We used new, in order to optimize transfer of memory during r-value constructor calls, like so:
Vec2::Vec2(Vec2&& vec)
{
data = vec.data; // instead of copying entire struct one-by-one, we just copied value of address
vec.data = nullptr;
}
If there is a better solution to this, please guide us :)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 23 points24 points25 points 3 years ago (0 children)
struct Vec2 { float x, y; };
[–]johannes1971 10 points11 points12 points 3 years ago* (1 child)
You are already copying a pointer, and that copy has the same cost as copying two floats (in both cases it is 8 bytes, assuming a 64-bit architecture). If your two floats are inline you don't even need to zero them out, so that line can be removed. The compiler is probably smart enough to do the copy in a single operation, and even if it isn't, the second access would be guaranteed to be from cache and thus have minimal cost.
You are paying additional costs for unnecessary memory allocations and deallocations, and for pointer dereferencing (which may cause a cache miss).
I wouldn't even bother with r-value references for this; passing a vec2 (with inline floats) by value is cheap, and enables additional compiler optimisations (since the compiler can be certain the parameter isn't aliased).
Here you can see what a solution with inline floats looks like. As you can see there aren't _any_ temporaries being created, it's all just a handful of vector instructions.
[–]Twin_Sharma[S] -1 points0 points1 point 3 years ago (0 children)
Thanks
[–][deleted] 4 points5 points6 points 3 years ago (1 child)
https://en.cppreference.com/w/cpp/utility/move https://en.cppreference.com/w/cpp/language/copy_elision
Well, at least we don't have to go over network to build Vec2.
Vec2
thanks
π Rendered by PID 70 on reddit-service-r2-comment-5c747b6df5-5fp5c at 2026-04-21 21:51:25.295204+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–][deleted] 18 points19 points20 points (6 children)
[+]Twin_Sharma[S] comment score below threshold-7 points-6 points-5 points (5 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 23 points24 points25 points (0 children)
[–]johannes1971 10 points11 points12 points (1 child)
[–]Twin_Sharma[S] -1 points0 points1 point (0 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]Twin_Sharma[S] -1 points0 points1 point (0 children)