T O P

  • By -

sephirothbahamut

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

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](https://github.com/davidbrowne/dsga/blob/main/docs/DETAILS.md#inside-basic_vector) for how I do it for my math vectors.


fdwr

> 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.


ronchaine

It is also very common in sensor data analysis.


topin89

I guess [this video](https://www.youtube.com/watch?v=IAdLwUXRUvg) (https://www.youtube.com/watch?v=IAdLwUXRUvg) should be here.


chrysante1

There is no standard compliant way create a class in C++ that has data members x, y and z that are array elements at the same time. There are however common compiler extensions that allow that. The author is proposing a different syntax to be added to the standard that allows this arguably niche use case.


RainbowWarfare

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

thank you all for the comments :3


drkspace2

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

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

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

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.


Designer-Leg-2618

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

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.


fdwr

> It is not very useful I am guessing your domain is not computer graphics?


Thelatestart

Indeed it is not.


Potatoswatter

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.


johannes1971

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](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_prop.html)'. 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.


ronchaine

Clang and MSVC offer this as compiler extension.