Topic: Constant index operator


Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Wed, 2 Jun 2010 14:06:57 CST
Raw View
On May 8, 1:56 am, "joe.davis512" <joe.davis...@gmail.com> wrote:
> This is just a suggestion for a feature in C++. All it is, is a minor
> change, to allow better syntax support for tuples. It will allow
> selection of return type, based on the argument to the array indexing
> operator. Currently we have to have a separate function to access
> values within a tuple, e.g. std::get, my suggestion is why not be able
> to overload an operator for this. In my opinion this would give
> clearer syntax, and much easier to read code.
>
> Compare these two lines:
> double d = std::get<1> (tup);
> double d = tup[1];
>
> Which one is clearer?
> I understand that the verbosity of the first statement can, in some
> rare cases, outweigh the benefits of the second, but these are only
> very rare cases. How often do you not know the type of the variable
> you are accessing? (excluding use of RTTI)
>
> The suggested syntax for implementing this function is this:
> template <std::size_t N>
> return_type operator[] ();

Do you suggest to add a new keyword return_type?
The current replacement for that one would be

typename tuple_element<N, tuple<Types...> >::type&

(within namespace std) and would already return the
right type value. Thus, the problem to solve is *not* the
problem to auto-deduce the proper return type for this
operator[] overload, as your previous statement implies.
Except for the fact that the operator[] overload is missing
the currently required function parameter, this is already
a valid member function template declaration and would
be called like this (if we allow for the language extension
that this function parameter may be omitted):

double d = tup<1>[];

Surely this is not what you want and I don't see how the
wanted syntax

double d = tup[1];

could be realized even with such a language extension in
my mind. E.g. just requiring that

tup[1]

should be an "alias" for

tup<1>[]

is not an overwhelming convincing argument. It would e.g.
raise the question in which way the compiler should
resolve the problem, if a class provides both operator[]
overloads:

template<class... Types>
class tuple {
public:
 double operator[] (int); // Classic overload

 template <std::size_t N> // New Overload
 typename tuple_element<N, tuple<Types...>>::type& operator[] ();
};

> All I want to know, is what people think of this suggestion and
> whether it should be implemented in the next standard.

I sympathize with the basic idea, but without a more
specific proposal in which way this extension should
be realized it's hard to predict the chances of acceptance
by the C++ standard committee.

Greetings from Bremen,

Daniel Kr=FCgler



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 3 Jun 2010 14:43:06 CST
Raw View
On 2 Jun., 22:06, =?ISO-8859-1?Q?Daniel_Kr=FCgler?=
<daniel.krueg...@googlemail.c=.om> wrote:
> On May 8, 1:56 am, "joe.davis512" <joe.davis...@gmail.com> wrote:
>
> > This is just a suggestion for a feature in C++. All it is, is a minor
> > change, to allow better syntax support for tuples. It will allow
> > selection of return type, based on the argument to the array indexing
> > operator. Currently we have to have a separate function to access
> > values within a tuple, e.g. std::get, my suggestion is why not be able
> > to overload an operator for this. In my opinion this would give
> > clearer syntax, and much easier to read code.
>
> > Compare these two lines:
> > double d = std::get<1> (tup);
> > double d = tup[1];
>
> > Which one is clearer?
> > I understand that the verbosity of the first statement can, in some
> > rare cases, outweigh the benefits of the second, but these are only
> > very rare cases. How often do you not know the type of the variable
> > you are accessing? (excluding use of RTTI)
>
> > The suggested syntax for implementing this function is this:
> > template <std::size_t N>
> > return_type operator[] ();
>
> Do you suggest to add a new keyword return_type?
> The current replacement for that one would be
>
> typename tuple_element<N, tuple<Types...> >::type&
>
> (within namespace std) and would already return the
> right type value. Thus, the problem to solve is *not* the
> problem to auto-deduce the proper return type for this
> operator[] overload, as your previous statement implies.
> Except for the fact that the operator[] overload is missing
> the currently required function parameter, this is already
> a valid member function template declaration and would
> be called like this (if we allow for the language extension
> that this function parameter may be omitted):
>
> double d = tup<1>[];

Oops, I was a bit too quick here. This is of course not valid, only

double d = tup.operator[]<2>();

would be.

Sorry for any confusion and Greetings from Bremen,

- Daniel

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: restor <akrzemi1@gmail.com>
Date: Sat, 5 Jun 2010 21:59:18 CST
Raw View
> > > This is just a suggestion for a feature in C++. All it is, is a minor
> > > change, to allow better syntax support for tuples. It will allow
> > > selection of return type, based on the argument to the array indexing
> > > operator. Currently we have to have a separate function to access
> > > values within a tuple, e.g. std::get, my suggestion is why not be able
> > > to overload an operator for this. In my opinion this would give
> > > clearer syntax, and much easier to read code.

> > > Compare these two lines:
> > > double d = std::get<1> (tup);
> > > double d = tup[1];
>
> > > The suggested syntax for implementing this function is this:
> > > template <std::size_t N>
> > > return_type operator[] ();

> > Do you suggest to add a new keyword return_type?
> > The current replacement for that one would be

Hi, I believe the desired 'indexing' syntax would work, if the
templates were extended with the capability of deducing the values of
the non-template arguments from arguments that are passed to function.
The syntax for defining such thing could read:

 template< size_t I, typename... TupleArgs >
 auto
 std::tuple<TupleArgs...>::operator[]( size_t I ) // note the 'I'
 -> decltype( std::get<I>(*this) ); // return type

This syntax wold mean:
1. Deduce the value of the template argument from the function's
argument.
2. We are not interested in the value passed to the function, once we
deduced the value of the template arg.
3. The function (operator[] in our example) can only be called with
compile-time constants, similarly as the size of the array.

It would allow the usage like 'tuple[4]'. THis would make programming
with tuples more easy for normal people, but on the other hand may
cause confusion for others:
operator[] returning different types for different values?
Cannot iterate
 for( int i = 0; i < tuple.size() ; ++i ) tuple[i]; // error
??

Also, the only application of this potential feature is the accessor
function for tuples and perhaps to some meaprogramming libraries like
MPL or Fusion. This application may be too narrow to justify the
addition of a new language feature.

Regards,
&rzej




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Olaf Krzikalla <krzikalla@gmx.de>
Date: Mon, 7 Jun 2010 19:06:18 CST
Raw View
restor schrieb:

> Also, the only application of this potential feature is the accessor
> function for tuples and perhaps to some meaprogramming libraries like
> MPL or Fusion. This application may be too narrow to justify the
> addition of a new language feature.
>
Depends. I vaguely remember that I raised a similiar issue here about 10
years ago (sorry, couldn't find anything anymore).
The general idea is to make the syntax separation layer between types and
values a little bit transsparent by allowing function arguments as template
arguments in inline function.
Thus e.g. you are able to static dispatch calls with enum constants (which
were my intention then).
Or you can do, what the OP of this thread has intended.
Or - by elaborating the idea a little bit further - you can even develop an
approach to distinguish between compile-time and runtime-known values.
Something like this:

class Matrix
{
 value_type operator[](constexpr int i)
 {
   // maybe specialized for certain values:
   return very_fast_static_dispatch<i>(...);
 }

 value_type operator[](int i)
 {
   return have_to_compute_slowly(i);
 }
};

I admit, that the usage area is rather narrow. However, it can transparently
improve performance. And that's something we should worry about.


Best regards
Olaf Krzikalla

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "joe.davis512" <joe.davis512@gmail.com>
Date: Fri, 7 May 2010 17:56:42 CST
Raw View
Firstly, don't flame this post, if I've posted in the wrong place just
tell me.

This is just a suggestion for a feature in C++. All it is, is a minor
change, to allow better syntax support for tuples. It will allow
selection of return type, based on the argument to the array indexing
operator. Currently we have to have a separate function to access
values within a tuple, e.g. std::get, my suggestion is why not be able
to overload an operator for this. In my opinion this would give
clearer syntax, and much easier to read code.

Compare these two lines:
double d = std::get<1> (tup);
double d = tup[1];

Which one is clearer?
I understand that the verbosity of the first statement can, in some
rare cases, outweigh the benefits of the second, but these are only
very rare cases. How often do you not know the type of the variable
you are accessing? (excluding use of RTTI)

The suggested syntax for implementing this function is this:
template <std::size_t N>
return_type operator[] ();

All I want to know, is what people think of this suggestion and
whether it should be implemented in the next standard.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]