Topic: operator comma overloading


Author: abiradla@hotmail.com
Date: Thu, 5 Oct 2006 10:07:19 CST
Raw View
Pete Becker a    crit :

>
> Overloading the comma operator leads to unreadable code. But you can get
> something similar with tuples:

I don't agree :)
(a,b,c)= func(x,y) is more clear than func(x,y,a,b,c)
>
> #include <iostream>
> #include <tuple>
> using std::cout;
> using std::tr1::tuple;
> using std::tr1::make_tuple;
> using std::tr1::tie;
>
> typedef int X; // for this example
>
> tuple<X, X, X> func()
> {
> return make_tuple(1, 2, 3);
> }
>
> int main()
> {
> X a = 0, b = 0, c = 0;
> cout << a << ", " << b << ", " << c << '\n';
> tie(a, b, c) = func();
> cout << a << ", " << b << ", " << c << '\n';
> return 0;
> }
>
> The call to func returns a tuple holding three values of type X. The
> call to tie returns a tuple that holds references to a, b, and c.
> Assigning the tuple returned by func to the tuple returned by tie copies
> the individual X values in the tuple returned by func to a, b, and c,
> respectively.

Nice idea. But tie(a, b, c) = func(); will copy the func output to a,b
and c which I want to avoid for a performance issu.

In fact, I'm writing a matlab to C++ translator. I already made a C
version of the Matlab functions.
a Matlab function with 2 inputs and 3 outputs can be translated in C
like this :

X x=0;
X y=1;
X a,b,c;
//start working to call the function func
X* argin[2];
X* argout[3];
argin[0]=&x;
argin[1]=&y;
argout[0]=&a;
argout[1]=&b;
argout[2]=&c;
func(argin,2,argout,3);

Which is unreadable to a matlab user and not very nice to a C++
programmer.
I wanted a simple syntaxe very close to the syntaxe of Matlab :
[a,b,c]=func(x,y)

May be you have an idea  of a  smart #define to make the above code (8
lines to call a function) more readable ?
Can I do for example something like :
func(IN(x,y),OUT(a,b,c))
?
How do I define macro IN and OUT ?


---
[ 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: petebecker@acm.org (Pete Becker)
Date: Thu, 5 Oct 2006 15:49:56 GMT
Raw View
abiradla@hotmail.com wrote:
> Pete Becker a =E9crit :
>=20
>> Overloading the comma operator leads to unreadable code. But you can g=
et
>> something similar with tuples:
>=20
> I don't agree :)
> (a,b,c)=3D func(x,y) is more clear than func(x,y,a,b,c)

That's because in addition to overloading the comma operator it uses=20
invalid syntax.

> Can I do for example something like :
> func(IN(x,y),OUT(a,b,c))
> ?
> How do I define macro IN and OUT ?
>=20

make_tuple and tie., or maybe tie for both.

--=20

 -- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and=20
Reference." For more information about this book, see=20
www.petebecker.com/tr1book.

---
[ 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: Ron Natalie <ron@spamcop.net>
Date: Thu, 5 Oct 2006 10:55:57 CST
Raw View
abiradla@hotmail.com wrote:
> Pete Becker a    crit :
>
>> Overloading the comma operator leads to unreadable code. But you can get
>> something similar with tuples:
>
> I don't agree :)
> (a,b,c)= func(x,y) is more clear than func(x,y,a,b,c)

How about func(a,b,c) = func(x,y)?

---
[ 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: abiradla@hotmail.com
Date: Wed, 4 Oct 2006 11:11:32 CST
Raw View
Hello,

I want to overloading the comma operator in order to change the syntaxe
of some functions to make them more readables.
For example the  function func1 has 2 inputs and 3 output :
void func1(X x, X y, X &a, X &b, X &c)
{
a=x+y;
b=x-y;
c=x*y;
}
// use it
X x=-2;
X y=5;
X a,b,c;
func1(x,y,a,b,c);

I want to change this function to make it looks like this (if
possible):

XX func2(XX inputs)
{
X x =inputs(0);
X y =inputs(1);
X a,b,c ;
func1(x,y,a,b,c);
XX outputs = a, b,c ;//
return outpus;
}

//use it :
X x=-2;
X y=5;
X a,b,c;
(a, b,c ) = func2(x,y) // This the syntaxe that i want to have.

Is there any way to do that by defining the comma operator and a Class
XX as a collection of X type ?

Best regards ,

yaz

---
[ 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: Ron Natalie <ron@spamcop.net>
Date: Wed, 4 Oct 2006 11:57:20 CST
Raw View
abiradla@hotmail.com wrote:
> Hello,
>
> I want to overloading the comma operator in order to change the syntaxe

Overloading the comma operator is one of the stupidest features in C++.


> (a, b,c ) = func2(x,y) // This the syntaxe that i want to have.
>
The stuff to the left of the = sign is doable.  If X , X
returned an XX type (and XX,X or X,XX did as well) then the
assignment would be to an rvalue XX object which presumably
would whack the individual objects in the collection.

The right hand side you can't do.  The , in the func2(x,y)
isn't the comma operator, it's part of the syntax of the function
call.  You could do:
 func((x,y))

---
[ 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: petebecker@acm.org (Pete Becker)
Date: Wed, 4 Oct 2006 19:19:58 GMT
Raw View
abiradla@hotmail.com wrote:
>
> //use it :
> X x=-2;
> X y=5;
> X a,b,c;
> (a, b,c ) = func2(x,y) // This the syntaxe that i want to have.
>
> Is there any way to do that by defining the comma operator and a Class
> XX as a collection of X type ?
>

Overloading the comma operator leads to unreadable code. But you can get
something similar with tuples:

#include <iostream>
#include <tuple>
using std::cout;
using std::tr1::tuple;
using std::tr1::make_tuple;
using std::tr1::tie;

typedef int X; // for this example

tuple<X, X, X> func()
{
return make_tuple(1, 2, 3);
}

int main()
{
X a = 0, b = 0, c = 0;
cout << a << ", " << b << ", " << c << '\n';
tie(a, b, c) = func();
cout << a << ", " << b << ", " << c << '\n';
return 0;
}

The call to func returns a tuple holding three values of type X. The
call to tie returns a tuple that holds references to a, b, and c.
Assigning the tuple returned by func to the tuple returned by tie copies
the individual X values in the tuple returned by func to a, b, and c,
respectively.

For more details, see chapter 1 of my book, "The Standard C++ Library
Extensions."

--

 -- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.

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