Topic: operator []
Author: llewelly.at@xmission.dot.com (llewelly)
Date: Sat, 15 May 2004 21:25:37 +0000 (UTC) Raw View
andreytarasevich@hotmail.com (Andrey Tarasevich) writes:
> Mario Charest wrote:
>> ...
>> I wonder what is the rational for not supporting overload for more then one
>> index, like operator[][], operator[][][] etc.
>> ...
>
> Because it doesn't agree with basic C++ semantics. In C++ each '[]' is a
> separate operator. And in C++ you cannot apply several operators to one
> operand.
But the OP never suggested 'applying several operand to one
operand'. If you have more than one index, you would use
a::operator[][] like this:
a b;
b[1][2];
Such an expression contains 3 operands.
> Instead, each consecutive operator is applied to the result of
> the previous one. Within the bounds of this approach, overloads like
> 'operator[][]' simply make no sense.
[snip]
This is like arguing that templates 'don't make sense' within the
bounds of pre-template C++ .
Stroustrup discusses the possibility of providing overloading of
composite operators in D&E, 11.6.3 . The primary reason they
didn't make it into C++ was that he never found the time to
sufficiently develop the idea.
Since then, people have had substantial success using expression
templates and proxies to emulate such a feature, but IMO such
techniques are not accessible to the majority of C++
programmers. Then again, the majority of C++ programmers probably
have little need for overloading composite operators.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: postmaster@127.0.0.1 ("Mario Charest")
Date: Sun, 9 May 2004 03:29:06 +0000 (UTC) Raw View
Hi,
I wonder what is the rational for not supporting overload for more then one
index, like operator[][], operator[][][] etc.
- Mario
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: brok@rubikon.pl (Bronek Kozicki)
Date: Sun, 9 May 2004 18:42:51 +0000 (UTC) Raw View
On Sun, 9 May 2004 03:29:06 +0000 (UTC), "Mario Charest" wrote:
> I wonder what is the rational for not supporting overload for more then one
> index, like operator[][], operator[][][] etc.
operator[] may return reference to class with overloaded operator[]. See
example below - overloaded matrix::operator[] returns reference to row.
No special form of operator[] is necessary to gain this functionality.
Additional benefit is clear separation of resposibilites, thus allowing
more flexible design (eg. I can imagine extending it to support large
sparse matrix).
B.
#include <iostream>
template <typename Type, size_t Size>
class row
{
Type data_[Size];
public:
row()
{
for (int i = 0; i < Size; ++i)
data_[i] = Type();
}
Type& operator[] (size_t i)
{
return data_[i];
}
const Type& operator[] (size_t i) const
{
return data_[i];
}
};
template <typename Type, size_t Rows, size_t Cols = Rows>
class matrix
{
row<Type, Cols> rows_[Rows];
public:
row<Type, Cols>& operator[] (size_t i)
{
return rows_[i];
}
const row<Type, Cols>& operator[] (size_t i) const
{
return rows_[i];
}
};
int main()
{
static const int rows = 4;
static const int cols = 3;
matrix<int, rows, cols> m;
for (int j = 0; j < rows; ++j)
for (int i = 0; i < cols; ++i)
m[j][i] = i * 2 + j * j;
for (int j = 0; j < rows; ++j)
{
for (int i = 0; i < cols; ++i)
std::cout << m[j][i] << "\t";
std::cout << std::endl;
}
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net (James Kuyper)
Date: Mon, 10 May 2004 23:21:07 +0000 (UTC) Raw View
postmaster@127.0.0.1 ("Mario Charest") wrote in message news:<qX6nc.83388$j11.1517934@weber.videotron.net>...
> Hi,
>
> I wonder what is the rational for not supporting overload for more then one
> index, like operator[][], operator[][][] etc.
The basic reason is that C++ doesn't have any operators that would
correspond to those overloads. That's because C++ doesn't really have
multi-dimensional arrays; what it has is arrays of arrays. Indexing
into an array of arrays is done by repeated applications of
operator[], rather than by a single application of operator[][].
With a bit of cleverness, you can use an overloaded operator[] to
implement just about anything that you might reasonable want to put
into an operator[][] overload. The key thing you have to keep in mind
is the type of the intermediate results. For example:
template <class T> rowVector<T> &Array<T>::operator[](int row);
// rowVector is a class that acts like a reference
// to a particular row in an Array.
template<class T> T& rowVector<T>::operator[](int col);
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nagle@animats.com (John Nagle)
Date: Tue, 11 May 2004 18:22:51 +0000 (UTC) Raw View
James Kuyper wrote:
> postmaster@127.0.0.1 ("Mario Charest") wrote in message news:<qX6nc.83388$j11.1517934@weber.videotron.net>...
>
>>Hi,
>>
>>I wonder what is the rationale for not supporting overload for more then one
>>index, like operator[][], operator[][][] etc.
>
>
> The basic reason is that C++ doesn't have any operators that would
> correspond to those overloads. That's because C++ doesn't really have
> multi-dimensional arrays; what it has is arrays of arrays. Indexing
> into an array of arrays is done by repeated applications of
> operator[], rather than by a single application of operator[][].
C doesn't have multi-dimensional arrays. C++ could.
I've argued previously that "operator[]" should have the
same syntax as "operator()". There's a quirk in the syntax.
"foo(a,b)" is a two argument call, but "foo[a,b]" is
an invocation of the comma operator, and is equivalent
to "foo[b]".
I once asked Bruce Baumgart at IBM Almaden, who has
access to a huge archive of open source code, to grep
for occurences of "," within "[..]". We found about
600 occurences, all of which were either in comments,
in Microsoft language extensions, or errors. None
were intentional uses of the comma operator within
square brackets. So it is safe to do this. Any inadvertant
use of the comma operator in such a context would
show up as a compile time error.
The idea is not to provide multidimensional
arrays in C++ itself, but to allow overloads of
"operator[]" with multiple arguments. This is useful
for classes which implement constructs like sparse
arrays, where there's no convenient representation
of one row of the array.
Current practice is to implement sparse arrays
using "operator()" for multidimensional subscripting,
which results in code like
tab(a,b) = 1;
That's clearly a workaround for a language problem
and should be fixed.
John Nagle
Animats
[Incidentally, overloading the comma operator won't
solve this problem safely. Making the comma operator
return a bound pair breaks other uses of the comma operator
outside subscripts.]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: andreytarasevich@hotmail.com (Andrey Tarasevich)
Date: Thu, 13 May 2004 03:36:19 +0000 (UTC) Raw View
Mario Charest wrote:
> ...
> I wonder what is the rational for not supporting overload for more then one
> index, like operator[][], operator[][][] etc.
> ...
Because it doesn't agree with basic C++ semantics. In C++ each '[]' is a
separate operator. And in C++ you cannot apply several operators to one
operand. Instead, each consecutive operator is applied to the result of
the previous one. Within the bounds of this approach, overloads like
'operator[][]' simply make no sense.
Once again, in C++ each consecutive operator is applied to the result of
the previous one. This is a hint that might help you to find a way to
implement mutiple-indexing functionality (others have already suggested
possible solutions).
--
Best regards,
Andrey Tarasevich
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]