Topic: Comma operator and argument lists?
Author: nickt@kipling.aus.deuba.com (Nick Thurn)
Date: Mon, 18 Jun 2001 04:31:35 GMT Raw View
Sef Campstein wrote:
> > R operator()() { return fn(a1,a2,a3...aN); }
> > };
> >
> > Unfortunately the comma operator doesn't work in this context.
> > The overloaded commas have to be grouped by parenthese which
> > defeats the collapse of the argument list as it must be explicit.
> >
> > Ok my question:
> >
> > Why is this so? Historical? Grammar explosion?
> >
>
> I see no call to a comma operator in the code above. What is your question?
>
Ok, I'll try again ;-).
template <class T> struct arg { //... };
template <> struct arg<void> { //... }; // specialization for T=void
template <class T> T& operator, (T& t, arg<void>)
{ return t; }
given a function accepting 3 args, say:
template <class A1,class A2,class A3>
void fn(A1 a1,A2 a2,A3 a3)
{ //... }
and given, say:
arg<X1> x1;
arg<X2> x2;
arg<X3> x3;
and
arg<void> v1;
arg<void> v2;
then:
fn(x1,x2,x3,v1,v2);
COULD invoke the comma operator to collapse the argument list
to simply (x1,x2,x3) except the comma operator doesn't work that
way and would have to be invoked as:
fn(x1,x2,(x3,v1,v2));
which would work but requires me to know fn accepts 3 arguments
which means I simply call fn(x1,x2,x3) instead as the extra
work is pointless.
My question was why was the comma operator defined to work this
way? The D&E states that BS added overloading of it at the request
of Margaret Ellis but didn't see any great utility other than
completeness. This doesn't answer my question so I'm asking here.
cheers
Nick nick.thurn@db.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.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 18 Jun 2001 04:59:12 GMT Raw View
Nick Thurn wrote:
...
> My question was why was the comma operator defined to work this
> way? The D&E states that BS added overloading of it at the request
It's not the comma operator that's relevant to your question; the commas
you're talking about are part of the function call operator. So your
real question boils down to "why aren't the commas in a function call
affected by an operator,() overload?". The answer is the same reason
why overloading the "<" operator has no effect on uses of "<<".
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: nickt@kipling.aus.deuba.com (Nick Thurn)
Date: Mon, 18 Jun 2001 23:53:59 GMT Raw View
James Kuyper Jr. wrote:
> Nick Thurn wrote:
> ...
> > My question was why was the comma operator defined to work this
> > way? The D&E states that BS added overloading of it at the request
>
> It's not the comma operator that's relevant to your question; the commas
> you're talking about are part of the function call operator. So your
> real question boils down to "why aren't the commas in a function call
> affected by an operator,() overload?". The answer is the same reason
> why overloading the "<" operator has no effect on uses of "<<".
>
Ok but << is a separate operator/token to <. So I don't think it is the
same thing. Commas in argument lists are always separated by non-commas
so there is no ambiguity as with your example. If you like my question
can be rephrased - why are commas in function calls different to commas
in expressions. Note you can have
blathertype x = 1, 2, 3, 4;
and have the commas overloaded (see Blitz matrix definitions). So why
not fn(a,b,c) ?
cheers
Nick nick.thurn@db.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.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 19 Jun 2001 04:32:07 GMT Raw View
Nick Thurn wrote:
...
> Ok but << is a separate operator/token to <. So I don't think it is the
> same thing. Commas in argument lists are always separated by non-commas
> so there is no ambiguity as with your example. If you like my question
> can be rephrased - why are commas in function calls different to commas
> in expressions. Note you can have
Because if they were treated as comma operators, you could never have a
function taking more than one argument. The comma operators would cause
all of the arguments except the last one to be evaluated and then
discarded. Remember, the comma operator is always defined for any pair
of operands, regardless of type.
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: nickt@kipling.aus.deuba.com (Nick Thurn)
Date: Tue, 19 Jun 2001 16:45:32 GMT Raw View
James Kuyper Jr. wrote:
> Nick Thurn wrote:
> ...
> > Ok but << is a separate operator/token to <. So I don't think it is the
> > same thing. Commas in argument lists are always separated by non-commas
> > so there is no ambiguity as with your example. If you like my question
> > can be rephrased - why are commas in function calls different to commas
> > in expressions. Note you can have
>
> Because if they were treated as comma operators, you could never have a
> function taking more than one argument. The comma operators would cause
> all of the arguments except the last one to be evaluated and then
> discarded. Remember, the comma operator is always defined for any pair
> of operands, regardless of type.
>
Ok I'm starting to get the picture, thanks.
cheers
Nick
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "Sef Campstein" <sef@campst.tmfweb.nl>
Date: Sun, 17 Jun 2001 14:10:12 GMT Raw View
>
> A single template to a function and a variable number
> of arguments could then be created as say:
>
> template <class R, class Fn, class A1=void, ... class AN=void>
> struct functor {
> Fn fn;
> Argument<A1> a1;
> Argument<A2> a2;
> //...
> Argument<AN> aN;
>
> R operator()() { return fn(a1,a2,a3...aN); }
> };
>
> Unfortunately the comma operator doesn't work in this context.
> The overloaded commas have to be grouped by parenthese which
> defeats the collapse of the argument list as it must be explicit.
>
> Ok my question:
>
> Why is this so? Historical? Grammar explosion?
>
I see no call to a comma operator in the code above. What is your question?
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: nickt@kipling.aus.deuba.com (Nick Thurn)
Date: Thu, 14 Jun 2001 00:34:12 GMT Raw View
Hi Folks,
One solution for creating a generic functor with arguments
would involve using something like:
template <class T> struct Argument { T value; operatorT&() //... };
template <> struct Argument<void> {};
template <class T>
T& operator,(T& t, Argument<void>) { return t; }
A single template to a function and a variable number
of arguments could then be created as say:
template <class R, class Fn, class A1=void, ... class AN=void>
struct functor {
Fn fn;
Argument<A1> a1;
Argument<A2> a2;
//...
Argument<AN> aN;
R operator()() { return fn(a1,a2,a3...aN); }
};
Unfortunately the comma operator doesn't work in this context.
The overloaded commas have to be grouped by parenthese which
defeats the collapse of the argument list as it must be explicit.
Ok my question:
Why is this so? Historical? Grammar explosion?
Thanks in advance.
cheers
Nick nick.thurn@db.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.research.att.com/~austern/csc/faq.html ]