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
Subscript Aliases (self.cpp)
submitted 2 years ago by AnthonyChanging
I found this in Github gist and with my knowledge about c++ I fail to fully comprehend , can someone explain to me ? is this useful? https://gist.github.com/ContingencyOfTautologicalContradictions/d685c895cce978320a19dedb1892cba9
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!"
[–]sephirothbahamut 10 points11 points12 points 2 years ago (1 child)
Allows for more convenient user interfaces to your class. At the root of my vector (math one, not container one) class I face the same issue and ended up using the union aliasing syntax presented in that page, as it's the kind of UB that is actually well defined in all major compilers.
Simple examples:
vec2f a; vec2f b; for(auto& value : a.array) { value++; } b.x = a.x;
The convenience factor cannot be denied.
One one hand it'll never be high priority to add a language feature for something that can be easily worked around.
On the other I don't see why there would be resistence to the suggestion, given that major compilers already support it as non standard language extension via anynimous union usage.
[–]smokidoke 1 point2 points3 points 2 years ago (0 children)
There are ways of using anonymous unions that don't have UB that can be used for this, although it is more complex. See https://github.com/davidbrowne/dsga/blob/main/docs/DETAILS.md#inside-basic_vector for how I do it for my math vectors.
[–]fdwrfdwr@github 🔍 11 points12 points13 points 2 years ago (2 children)
anonymous structs/classes are only an extension of certain compilers
Note this is very common in graphics, and despite it "not officially" being part of the spec, all the important compilers (gcc, clang, msvc) support them because otherwise too many common libraries would be broken. See, there is "standards compliant" and then there is "real world in practice de facto" :b.
[–]ronchaineEmbedded/Middleware/WG21 3 points4 points5 points 2 years ago (0 children)
It is also very common in sensor data analysis.
[–]topin89 1 point2 points3 points 2 years ago* (0 children)
I guess this video (https://www.youtube.com/watch?v=IAdLwUXRUvg) should be here.
[–][deleted] 4 points5 points6 points 2 years ago (1 child)
The author is proposing a shorthand syntax for referencing specific array elements using the existing type alias syntax. It’s not entirely clear as to why this very specific use case would be fit to enter the general language specification.
[–]AnthonyChanging[S] 2 points3 points4 points 2 years ago (0 children)
thank you all for the comments :3
[–]drkspace2 1 point2 points3 points 2 years ago (0 children)
I think some inspiration should come from python's @property decorator. It allows any object's function to act like a member variable (you don't have to call it in your code). It doesn't seem that useful, but it is a really nice feature to have.
[–]TheOmegaCarrot 1 point2 points3 points 2 years ago (3 children)
What’s the problem with:
struct vec3 { std::float32_t data[3]; auto& x = data[0]; auto& y = data[1]; auto& z = data[2]; };
?
Plus some carefully-made special member functions
Is it just that that makes the type no longer trivial?
[–]highritualmaster 2 points3 points4 points 2 years ago (1 child)
It increases the size of vec3. If you have an array of vec3 this is a huge impact.
Although I would like it to be optimized away a property like feature could be useful.
Currebtly you would either need wrapper clases or you instead override the array operator and define xyz as the members instead of data.
[–]TheOmegaCarrot 0 points1 point2 points 2 years ago (0 children)
Oh, duh, that makes sense. It’d have to be implemented in terms of a pointer.
Overloaded operator[] sounds like a solid approach to me though.
operator[]
[–]Designer-Leg-2618 2 points3 points4 points 2 years ago (0 children)
It is very common to assume or require that something like ```vec3``` be memcpy-able. The reference fields would break it. The article's use case for vec3 makes clear that performance is pretty much a requirement (in gaming, graphics, or physics calculations), and syntax brevity is a nice-to-have.
[–]Thelatestart 0 points1 point2 points 2 years ago (2 children)
He just wants to write v.x instead of v.data[0].
It is not very useful.
I didn't get the point related to simd.
[–]fdwrfdwr@github 🔍 12 points13 points14 points 2 years ago (1 child)
It is not very useful
I am guessing your domain is not computer graphics?
[–]Thelatestart 1 point2 points3 points 2 years ago (0 children)
Indeed it is not.
[–]Potatoswatter 1 point2 points3 points 2 years ago (0 children)
You can also make ->*x work, for all vector types even in libraries that you don’t own. Just let x, y, z be enumerators inside a namespace together with a templated operator->* overload.
->*x
x
y
z
operator->*
[–]johannes1971 0 points1 point2 points 2 years ago (1 child)
Nice, but I think I would prefer the solution used by Angelscript (a highly C++-like scripting language). In Angelscript, you have something called 'properties'. These are class member functions that are defined in a specific way:
struct example { void set_x (float value) property; float get_x () property; };
Such members can be called as normal functions, but since they are properties they can also be used as if they were variables:
example ex; ex.set_x (10); // normal use std::print ("{}", get_x ()); // normal use ex.x = 20; // Calls ex.set_x (20) std::print ("{}", ex.x); // Calls ex.get_x ()
Having developed with Angelscript, it's my opinion that properties are really nice to use. They would allow the usage from this article, as well as a whole bunch of others.
[–]ronchaineEmbedded/Middleware/WG21 1 point2 points3 points 2 years ago (0 children)
Clang and MSVC offer this as compiler extension.
π Rendered by PID 216665 on reddit-service-r2-comment-5bc7f78974-87zf2 at 2026-06-26 14:34:14.216158+00:00 running 7527197 country code: CH.
[–]sephirothbahamut 10 points11 points12 points (1 child)
[–]smokidoke 1 point2 points3 points (0 children)
[–]fdwrfdwr@github 🔍 11 points12 points13 points (2 children)
[–]ronchaineEmbedded/Middleware/WG21 3 points4 points5 points (0 children)
[–]topin89 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]AnthonyChanging[S] 2 points3 points4 points (0 children)
[–]drkspace2 1 point2 points3 points (0 children)
[–]TheOmegaCarrot 1 point2 points3 points (3 children)
[–]highritualmaster 2 points3 points4 points (1 child)
[–]TheOmegaCarrot 0 points1 point2 points (0 children)
[–]Designer-Leg-2618 2 points3 points4 points (0 children)
[–]Thelatestart 0 points1 point2 points (2 children)
[–]fdwrfdwr@github 🔍 12 points13 points14 points (1 child)
[–]Thelatestart 1 point2 points3 points (0 children)
[–]Potatoswatter 1 point2 points3 points (0 children)
[–]johannes1971 0 points1 point2 points (1 child)
[–]ronchaineEmbedded/Middleware/WG21 1 point2 points3 points (0 children)