C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Subscript aliasing

From: veronica alphonso <verodeving_at_[hidden]>
Date: Thu, 7 Mar 2024 23:05:55 +0000
An example:

void foo()
{
    vec3 v;
    v.x = 42.f; // same as 'v.x() = 42.f;'
    float x_copy = v.x; // same as 'float x_copy = v.x();'
}

El jue, 7 mar 2024 a las 23:02, veronica alphonso (<verodeving_at_[hidden]>)
escribió:

> .. an extension of certain compilers, and straight illegal in other
> compilers.
>
> The only standard-compliant of doing something similar is with the
> following:
>
> class vec3
> {
> float my_vec[3];
> public:
> [[nodiscard]] constexpr float& x(){
> return my_vec[0];
> }
>
> [[nodiscard]] constexpr float& y(){
> return my_vec[1];
> }
>
> .....
> };
>
> But i see this approach less than ideal (writing () per member access
> might be a little tough). Hence, i propose the following syntax:
>
> class vec3
> {
> float my_vec[3];
> public:
> using x = my_vec[0];
> using y = my_vec[1];
> ....
> };
>
> which would serve as a referencing mechanism to the specified elements of
> the array, similar to the previous method.
>
> El jue, 7 mar 2024 a las 22:54, veronica alphonso (<verodeving_at_[hidden]>)
> escribió:
>
>> In C++ there isn't a standard-compliant way of creating named aliases of
>> c-like array elements. In certain contexts, it is preferred to access these
>> these array elements with a named identifier that is bounded to that
>> particular array entry, e.g:
>>
>> union
>> {
>> float my_vec[3];
>> struct
>> {
>> float x, y, z;
>> };
>> };
>>
>> However, anonymous structs/classes are only
>>
>

Received on 2024-03-07 23:06:07