Viability of `:` as range constructor (as a new language feature) and usage for slicing

47 views
Skip to first unread message

gideon.m...@gmail.com

unread,
Jan 16, 2018, 4:54:55 PM1/16/18
to ISO C++ Standard - Discussion
Before fleshing out this idea, I wanted to float it here in order to get some remarks and comments.

From the cppreference (http://en.cppreference.com/w/cpp/experimental), the proposal (https://ericniebler.github.io/std/wg21/D4128.html), as well as Eric Niebler's talk (https://www.youtube.com/watch?v=mFUXNMfaciE), I'm still not getting a clear understanding of how one might use these ranges for slicing of arrays with views. However, I believe ranges could later be used quite nicely for slicing, somewhat like http://en.cppreference.com/w/cpp/numeric/valarray/slice, but far more powerful.
If a range view can be used as a parameter to a custom `operator[](...)`, this could be used for powerful custom operations on data arrays, much in the style of Python's slicing.
However, the syntax for such slicing is currently still annoying, where the best would look something like `vec[{0,5}]` and ranges would make this more powerful, but not prettier.

The suggestion:
Introduce something a language feature around `:` (maybe only in conjunction with integers), possibly simply as a constructor for a range, so that e.g.

x[0:5]
y
[:3]
z
[3:]

or with the `operator()(...)` enabling multi-dimensional slicing, quite like in python:

x(:,3)   // instead of x(std::all, 3)
y
(:,0:3) // instead of y(std::all, {0,3})
z
(:5,:5)


Adding such Pythonesque syntax would, in my opinion, greatly ease the usage of arrays in context of numerical calculations. Since the new ranges feature is slowly coming together, it seems to me this could be a powerful addition to the language

Nicol Bolas

unread,
Jan 16, 2018, 5:37:49 PM1/16/18
to ISO C++ Standard - Discussion, gideon.m...@gmail.com
This is the forum for discussing the standard as it currently stands, not for proposed changes to it.

On Tuesday, January 16, 2018 at 4:54:55 PM UTC-5, gideon.m...@gmail.com wrote:
Before fleshing out this idea, I wanted to float it here in order to get some remarks and comments.

From the cppreference (http://en.cppreference.com/w/cpp/experimental), the proposal (https://ericniebler.github.io/std/wg21/D4128.html), as well as Eric Niebler's talk (https://www.youtube.com/watch?v=mFUXNMfaciE), I'm still not getting a clear understanding of how one might use these ranges for slicing of arrays with views. However, I believe ranges could later be used quite nicely for slicing, somewhat like http://en.cppreference.com/w/cpp/numeric/valarray/slice, but far more powerful.
If a range view can be used as a parameter to a custom `operator[](...)`, this could be used for powerful custom operations on data arrays, much in the style of Python's slicing.

This is wrong. And not reasonably possible.

The whole idea of range adaptors is that they can act on any range (within some constraint). They're not restricted to ranges that implement some special member function; if it is a range, the adapter can adapt it.

You cannot impose a member function onto some other type. You certainly can't impose `operator[]` on some other type. Nor should you be able to. So the only way to do what you want is to make every range type implement slicing.

This is the exact opposite of how range adaptors work.

Slicing is merely one kind of range adaptor. There are many others, and I don't see slicing being so common that it should have specialized syntax for invoking it.
Reply all
Reply to author
Forward
0 new messages