2

In C++/CLI, you can specify the following for multidimensional arrays.

array<int, 2>^ Foo = gcnew array<int, 2>(10);
Foo[0, 0] = 1;
Foo[1, 0] = 2;
// ...

I'm trying to replicate the above in the closest syntax possible in standard C++ (C++11 is allowed) via a templated class called my_array.

e.g.

template <typename T, int rank = 1>
    class my_array { };

Is it possible via some comma operator overloading tricks to achieve C++/CLI's syntax under standard C++, along with overriding my_array's subscript operator?

Ideally, I'd like the my_array used this way (equivalent to the above example):

my_array<int, 2> Foo = // ... (ignore this part - already implemented)
Foo[0, 0] = 1;
Foo[1, 0] = 2;
// ...

In case anyone's wondering, I'm creating a C++/CLI equivalent for GCC and currently the framework does not support multidimensional arrays. I'm looking to add that functionality in the closest possible way syntax wise to C++/CLI.

Zach Saw
  • 4,308
  • 3
  • 33
  • 49
  • 1
    Why not simply use nested `std::vector` or `std::array`? It won't have the same syntax, but it's much less code you have to write. – Some programmer dude Nov 26 '12 at 12:43
  • `Foo[0][0]` is more intuitive than `Foo[0,0]` and you can achieve that with nested `std::array<>` or `std::vector<>`. – iammilind Nov 26 '12 at 12:49
  • Like I said, I'm creating C++/CLI for GCC. If you don't use std::array / std::vector with C++/CLI's managed pointers, you won't use it here too. – Zach Saw Nov 26 '12 at 12:52
  • @ZachSaw: Creating a C++/CLI language (it is a different language!) on top of plain C++ is a quite hard endeavor. You might want to consider dropping this issue (i.e. marking as *not currently supported*) and try to solve some of the harder issues as how are you going to integrate the garbage collector, how are you going to treat the managed references (`^`) or how to integrate your *managed* objects with managed languages like C#. – David Rodríguez - dribeas Nov 26 '12 at 13:16
  • @David: I won't be integrating with C#. C++/CLI is powerful enough to bring a lot of the modern idioms and design patterns into C++ without having to resort to have things like enabled_shared_from_this and pitfalls where one cannot use make_shared_from_this in the ctor. GC is already sorted via BoehmGC in precise mode (similar to how Mono and GJC use it). Anyway, I think I'll do as you said and mark it as not supported. Or, introduce a multidim_array with `Foo(0, 0) = 1` as the subscript syntax. – Zach Saw Nov 26 '12 at 13:22

2 Answers2

3

No it is not possible in standard C++. Indeed operator[] can only take a single argument.

You may achieve similar syntax using one of these solutions:

  • operator() with several arguments like array(i, j)
  • Use a proxy class for arguments, e.g. array[makeIndex(i, j)]
  • Using comma operator: array[makeIndex(i), makeIndex(j)]
  • Or array[IndexBegin, i, j].

See also this and that questions.

-- Optimization note --

In you go the comma route, you'll be building dynamic lists with the comma operator, and the array will be checking the length of these lists. In a naive implementation these checks will be run-time and redundant (when used in a loop).

Better option: use lists of statically known length (with templates) Like IndexBegin is IndexList<0>, IndexList<N> [comma] int is IndexList<N+1>. Then if your array also know its dimension statically, like a 2D array is Array<2> then you can check at compile time that a 2D array only accepts 2 indices.

Community
  • 1
  • 1
Antoine
  • 13,494
  • 6
  • 40
  • 52
  • It will work and didn't his 4th bullet point `array[IndexBegin, i, j]` cover that? I was just wondering if there's a more natural syntax than that. – Zach Saw Nov 26 '12 at 13:16
  • You can overload [] to work with ints and index-lists so you can use `array[i]` and only if there are multiple indices use `array[multi, i, j]`. But you won't be able to have the `array[i,j]` syntax. – Antoine Nov 26 '12 at 14:34
  • I was replying to Joachim Pileborg who said he didn't know if overriding the comma operator is possible. I replied by saying it will and that your 4th bullet point covers it. – Zach Saw Nov 26 '12 at 22:35
0

In C++23, you can use this syntax in standard C++.
Related Links:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2128r6.pdf https://eel.is/c++draft/over.sub

Kargath
  • 450
  • 5
  • 15