Topic: Comma within square brackets - not found in Google Code Search. Multi-argument "operator[]" is safe.
Author: "Me" <anti_spam_email2003@yahoo.com>
Date: Thu, 26 Oct 2006 00:24:36 CST Raw View
John Nagle wrote:
> 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.
>
> 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.
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://spirit.sourceforge.net/distrib/spirit_1_6_1/libs/spirit/phoenix/doc/statements.html
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: "Aleksey Loginov" <Aleksey.Loginov@gmail.com>
Date: Thu, 26 Oct 2006 09:45:57 CST Raw View
John Nagle wrote:
> 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].
>
Don't forget you can overload comma operator.
This can give interesting advantages.
Real life example (http://code.google.com/p/night-lambda/):
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.comeaucomputing.com/csc/faq.html ]
Author: djowel@gmail.com
Date: Mon, 30 Oct 2006 09:38:04 CST Raw View
John Nagle wrote:
> Me wrote:
> > 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://spirit.sourceforge.net/distrib/spirit_1_6_1/libs/spirit/phoenix/doc/statements.html
>
> 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.
That bit of wierdness is still in use today. boost::lambda borrowed the
syntax (see http://tinyurl.com/zjafx search for "The BLL supports an
alternative syntax for control expressions"). Then there's also
Phoenix-2 (http://tinyurl.com/b25hr). 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,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net
---
[ 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.comeaucomputing.com/csc/faq.html ]