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

This is a discussion on Comma within square brackets - not found in Google Code Search. Multi-argument"operator[]" is safe. - c++ ; 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 ...

+ Reply to Thread
Results 1 to 8 of 8

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

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

    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.comeaucomputing.com/csc/faq.html ]


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

    John Nagle wrote:

    > foo[a,b]
    >
    > is an invocation of the comma operator which ignores a
    > and is equivalent to


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

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


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

    >
    > 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.

    ---
    [ 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 ]


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

    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/distri...tatements.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 ]


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


    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 ]


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

    * Ron Natalie:
    > John Nagle wrote:
    >
    >> foo[a,b]
    >>
    >> is an invocation of the comma operator which ignores a
    >> and is equivalent to

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


    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.


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

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


    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.research.att.com/~bs/array34.c>.

    Or, for example, the standard's §8.3.4/4, «When 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.comeaucomputing.com/csc/faq.html ]


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

    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/distri...tatements.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.

    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.comeaucomputing.com/csc/faq.html ]


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

    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/distri...tatements.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 ]


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

    nagle@animats.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
    pete.forman@westerngeco.com -./\.- the opinion of Schlumberger or
    http://petef.port5.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.comeaucomputing.com/csc/faq.html ]


+ Reply to Thread

Similar Threads

  1. "Element not found" in Search Index Error Log
    By Application Development in forum Sharepoint
    Replies: 0
    Last Post: 10-03-2007, 01:20 PM
  2. Replies: 0
    Last Post: 09-13-2007, 11:26 AM
  3. Deleting message "Pattern not found" after a search run
    By Application Development in forum Editors
    Replies: 1
    Last Post: 07-06-2007, 06:27 PM
  4. Replies: 0
    Last Post: 10-15-2006, 03:27 PM
  5. Replies: 0
    Last Post: 10-15-2006, 03:27 PM