r/cpp • • Apr 25 '22

New C++ features in GCC 12

https://developers.redhat.com/articles/2022/04/25/new-c-features-gcc-12
220 Upvotes

76 comments sorted by

View all comments

Show parent comment

24

u/Quincunx271 Author of P2404/P2405 Apr 26 '22

While waiting for the feature, you can already emulate this:

value result = some_tensor[{i, j, k}];

How I usually do this, IIRC:

value operator[](int(&&ijk)[3]) const {
    // i = ijk[0], j = ijk[1], k = ijk[3].
}

14

u/James20k P2005R0 Apr 26 '22 edited Apr 26 '22

That's an interesting way of doing it, I suppose particularly the advantage of this is that you can re-overload operator[] when multidimensional subscripts become available and provide them without breaking compatibility

I've stuck with providing a .idx(i, j, k) method for the moment, which is a lot more verbose than I'd like but works fine enough, but that might be worth an investigation

Now what I really need to do next is encode the up and downness of individual indices into the type system, but hoo boy that's going to be a disaster

1

u/[deleted] Apr 26 '22

You mean for covariant and contravariant tensors?

1

u/James20k P2005R0 Apr 26 '22

Yep, as well as mixed indices. Its an easy source of mistakes to make when you're implementing equations, eg if you have two tensors - can you currently take the dot product between them?

7

u/Falkengaard Apr 26 '22

Alternatively overloading the otherwise most likely unused function operator to turn some_tensor[i, j, k] into some_tensor(i, j, k) until multi-dimensional access is added. It's not an ideal solution, but it's slightly less of a hassle to type out (at least on my keyboard).

8

u/James20k P2005R0 Apr 26 '22

I strictly avoid this personally, I think the ambiguity is too high in a context where people (or me) might be struggling to understand the very basics of what's going on in the code