Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by Ron Natali » Fri, 27 Oct 2006 00:47:39



It doesn't ignore it, it evaluates it and then uses the
right hand operand to determine the value of the expression.


There's no such thing as a multidimensional array in C++. Just
arrays of arrays.


Or how about the fact that C and C++ have never had that syntax.

---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by nagl » Fri, 27 Oct 2006 03:24:45

One of the old issues that's come up a few times is whether
"operator[]" should have the same argument syntax as "operator()"
or regular function call syntax. Currently,

foo(a,b)

is a two-argument function call, while

foo[a,b]

is an invocation of the comma operator which ignores a
and is equivalent to

foo[b].

As a result, "operator[]" can only take one argument. This
prohibits the creation of multidimensional array classes using
standard bracket syntax.

The justification for this is protection of legacy code which
might conceivably use the comma operator within square brackets.

But now we have Google Code Search, and can easily look for
such usages. A search key to use is:

^[^\"/]*\[[^\[\]\(\),]*,[^\[\]\(\),]*\] lang:c++

This isn't perfect, but comes as close as we can get with regular expressions.
(The main problem is throwing out comments and quotes; there are about 12,000
occurences of commas inside square brackets inside quotes or comments.)

This gets us about 100 hits.

They're all in multline comments, Microsoft "@deftypefn" statements,
char constants, template type arguments or embedded assembler.

Try the search yourself. Commas inside subscript expressions just
aren't being used in the visible universe of C++ code.

At last, we have the tools to dispose of this question.

John Nagle
Animats

---
[ 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.yqcomputer.com/ ]

 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by Me » Fri, 27 Oct 2006 09:24:36


There are lambda and expression template libraries that abuse the array
index operator because it relies on the fact that it doesn't behave
like the function call operator. The immediate example that comes to
mind:

http://www.yqcomputer.com/

---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by Aleksey Lo » Fri, 27 Oct 2006 18:45:57


Don't forget you can overload comma operator.
This can give interesting advantages.
Real life example ( http://www.yqcomputer.com/ ):

namespace pvt {

struct arg {} _1;

template<typename L, typename R>
typename append<L,R>::type oprerator , ( L &, R & );

template<typename L, typename R>
typename append<L const,R>::type oprerator , ( L const &, R & );

template<typename L, typename R>
typename append<L,R const>::type oprerator , ( L &, R const & );

template<typename L, typename R>
typename append<L const,R const>::type oprerator , ( L const &, R
const & );

}

struct array {
result_type operator [] ( Tuple const & );
} a;

Now you can white
a [ _1, 10, _1 ]
which equivalent to
a [ tuple<arg, const int, const arg> (_1, 10, arg () ) ]

Note that you save const qualifiers of original arguments. In this case
it's not important, but sometimes you will need it.
It addition it's possible to build list of n-args without n overloads.

How can you archive it with operator ()?

---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by alfp » Sat, 28 Oct 2006 02:38:00

* Ron Natalie:


Right, although I suspect John meant this as an example of inadvertently
supplying two index values using syntax from some other language, in
which case the first value (no side effects) has no side effects and is
effectively ignored.



Again, I think that's a misunderstanding. First, about the terminology:

int a[3][4];

might be described as multidimensional array, by someone favoring that
term. For example, <url: http://www.yqcomputer.com/ ~bs/array34.c>.

Or, for example, the standard's .3.4/4, hen several "array of"
specifications are adjacent, a multidimensional array is created; ...".

Second, whatever we call the built-in features of the language, surely
that does not limit what we can call abstractions built upon those features.

>>> The justification for this is protection of legacy code which >>> might conceivably use the comma operator within square brackets. >> >> Or how about the fact that C and C++ have never had that syntax.

If I'm not very mistaken, that was the point. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by nagl » Sat, 28 Oct 2006 11:43:42


That bit of wierdness dates from 2002. Can you find a production use of
it in Google code search?

Now that we have a big code archive available, we no longer have to
speculate about what gets used and what doesn't. We can objectively
answer the question.

John Nagle
Animats

---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by djowe » Tue, 31 Oct 2006 18:38:04


That bit of wierdness is still in use today. boost::lambda borrowed the
syntax (see http://www.yqcomputer.com/ "The BLL supports an
alternative syntax for control expressions"). Then there's also
Phoenix-2 ( http://www.yqcomputer.com/ ). Call it "abuse" or whatever, but
the point is that you can't match the level of expressiveness with any
other syntax. The whole point with DSEL is that we attempt to get as
close as possible to the target syntax.

Regards,
---
[ 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.yqcomputer.com/ ]
 
 
 

Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.

Post by pete.forma » Wed, 01 Nov 2006 01:12:19


XXXX@XXXXX.COM (John Nagle) writes:

> Now that we have a big code archive available, we no longer have to
> speculate about what gets used and what doesn't. We can objectively
> answer the question.

At the risk of stating the obvious, that archive only holds public
source code.
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
XXXX@XXXXX.COM -./\.- the opinion of Schlumberger or
http://www.yqcomputer.com/ -./\.- WesternGeco.

---
[ 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.yqcomputer.com/ ]