C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can this be working ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Cyril
Guest





PostPosted: Thu Jun 23, 2005 11:04 pm    Post subject: How can this be working ? Reply with quote



Hi,

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

I thank you by advance for any explanation.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ferdi Smit
Guest





PostPosted: Fri Jun 24, 2005 7:10 am    Post subject: Re: How can this be working ? Reply with quote



"Cyril" <mail.pourri (AT) laposte (DOT) net> wrote

Quote:
Hi,

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

It works by overloading the comma operator. Yes, "operator," is an operator
that can be overloaded. A simple (incomplete) example for, say, a vector
would be this:

template <typename ScalarT, int Dim, int Index>
class VectorSetter {
public:
VectorSetter(Vector<ScalarT, Dim>& vec) : vec_(vec) {}

VectorSetter<ScalarT, Dim, Index+1> operator , (const ScalarT& value) {
vec_[Index] = value;
return VectorSetter<ScalarT, Dim, Index+1>(vec_);
}
private:
Vector<ScalarT, Dim>& vec_;
};

template <typename ScalarT, int Dim>
class VectorSetter<ScalarT, Dim, Dim> {
public:
VectorSetter(Vector<ScalarT, Dim>& vec) : vec_(vec) {}

void operator , (const ScalarT& value) {
CompileTimeAssert(false, Too_many_values_in_comma_initializer_list);
}

operator Vector<ScalarT, Dim>& () {
return vec_;
}
private:
Vector<ScalarT, Dim>& vec_;
};

template <
typename ScalarT,
int Dim
Quote:

class Vector {

public:
VectorSetter (*this)[0] = value;
return VectorSetter<ScalarT, Dim, 1>(*this);
}

// ...

}

I'm not saying this is a complete example, but it illustrates the point. Now
you can write code like this:

Vector<4, float> v = 1, 2, 3, 4;

The expression list uses a similar technique; with some additions. Note that
an expression such as Vector<4, float> = 1,2,3,4; is evaluated as
(((Vector<4,float> = 1) , 2), 3), 4. So by making the assignment operator
return a proxy or surrogate object that overloads the comma operator, (no
pun intended...) you can achieve the desired result.

For more information on the general case of expressions, you might want to
read the article "C++ Expression Templates" by Angelika Langer and Klaus
Kreft.


--
Regards,

Ferdi Smit
smit xs4all nl



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jun 24, 2005 7:12 am    Post subject: Re: How can this be working ? Reply with quote



* Cyril:
Quote:

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

That looks like a redefined comma operator.

Since the expression deals in temporaries and presumably builds up a list
of values, it's likely to be a bit inefficient, O(n^2) in general.

There are ways to avoid that cost, but unless the documentation was very
reassuring and explicit on that point I'd be suspicious.


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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Old Wolf
Guest





PostPosted: Fri Jun 24, 2005 7:20 am    Post subject: Re: How can this be working ? Reply with quote

Cyril wrote:
Quote:
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant

In C++ you can overload '=' and ',' for classes.
So that statement is like:

l.operator=(x).operator,(2).operator,(y).operator,(x+y);

Presumably operator= and operator, functions both return
a reference to l, so it is doing the same thing as:

l.operator=(x);
l.operator,(2);
l.operator,(y);
l.operator,(x+y);

Quote:
(I've tried this with int, and it compiles, however I cannot
save the other code), and how does it work ?

If , is not overloaded then it works by evaluating and then discarding
the left-hand operand.

If you wrote:
int p;
p = x, 2, y, x+y;

it's like:
p = x;
2;
y;
x+y;

Or if you wrote:
int p = x, 2, y, x+y;

it's like writing:
x;
2;
y;
int p = x+y;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Fri Jun 24, 2005 7:21 am    Post subject: Re: How can this be working ? Reply with quote

Cyril wrote:
Quote:
Hi,

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

Operator overloading, and it's not the '=', it's the comma operator.

--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Heinz Ozwirk
Guest





PostPosted: Fri Jun 24, 2005 11:12 am    Post subject: Re: How can this be working ? Reply with quote


"Alf P. Steinbach" <alfps (AT) start (DOT) no> schrieb im Newsbeitrag
news:42bb45b7.139553796 (AT) news (DOT) individual.net...
Quote:
* Cyril:

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

That looks like a redefined comma operator.

Since the expression deals in temporaries and presumably builds up a list
of values, it's likely to be a bit inefficient, O(n^2) in general.

Why do you assume that? There is no ned for temporary objects. Actually, due
to operator precedence, temporary objects are quite difficult to introduce
into that expression. The expression actually is

l.operator=(x).operator,(2).operator,(y).operator,(x+y);

Assuming that lst::operator=(symbol) clears the list and inserts the symbol
as its only element, and lst::operator, basically calls lst's equivalent of
push_back, there are no temporaries involved and I don't see any
inefficiency, at least no non-linear one.


A final note to the original poster: "Don't do that at home". Never use 'l'
as a variable name, it is too easily read as 1. And don't write lst instead
of list. List is short enough. No need to shorten it beyond readability.

HTH
Heinz



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Fri Jun 24, 2005 11:13 am    Post subject: Re: How can this be working ? Reply with quote

"Cyril" <mail.pourri (AT) laposte (DOT) net> wrote

Quote:
Hi,

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

My guess is that they overloaded "," (comma) operator. Which is generally
not recommended. Streaming operators (<<, >>) are usually used for that
purpose.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
chris jefferson
Guest





PostPosted: Fri Jun 24, 2005 11:14 am    Post subject: Re: How can this be working ? Reply with quote

Cyril wrote:
Quote:
Hi,

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I am
wondering how that code can be C++ compliant (I've tried this with int,
and it compiles, however I cannot save the other code), and how does it
work ?

There are two seperate things at work here, both a little strange Smile


Normally , is used to seperate statements, and act mostly like ;

So for ints, this statement is (mostly) the same as l = x; 2; y; x+y;
If you try compiling that, you'll find it works fine, and the 2; y; x+y;
just doesn't do anything (that often suprises people at first).

However, with this symbol and list, I suspect that writers have
overloaded the comma operator so that it can be used to build lists.
This is a nice trick which I've seen a few times.

Chris

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jun 24, 2005 3:40 pm    Post subject: Re: How can this be working ? Reply with quote

* Heinz Ozwirk:
Quote:

* Alf P. Steinbach:

Since the expression deals in temporaries and presumably builds up a list
of values, it's likely to be a bit inefficient, O(n^2) in general.

Why do you assume that? There is no ned for temporary objects. Actually, due
to operator precedence, temporary objects are quite difficult to introduce
into that expression.

My bad, I used an incorrect grammar (effective precedence) in my parsing.

Thanks for the correction.

Mumble, mumble, red-hot volcanic ashes raining, mumble, people who redefine
operators, mumble, mumble, mumble, those redefining _comma_ operators, mumble.

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Jun 24, 2005 3:45 pm    Post subject: Re: How can this be working ? Reply with quote

chris jefferson wrote:
Quote:
Cyril wrote:

While browsing to the Ginac website (
http://www.ginac.de/tutorial/tutorial_4.html#SEC24 )
I've just found this code :

symbol x("x"), y("y");
lst l;
l = x, 2, y, x+y;

l is a list, and the l = x, 2, y, x+y line adds expressions to the list
(so after that line, l owns 4 expressions which are 'x', '2', 'y' and
'x+y');

It is the first time I'm seeing this for a = operator, so I
am wondering how that code can be C++ compliant (I've tried
this with int, and it compiles, however I cannot save the
other code), and how does it work ?

There are two seperate things at work here, both a little strange :)

Normally , is used to seperate statements, and act mostly like ;

So for ints, this statement is (mostly) the same as l = x; 2;
y; x+y; If you try compiling that, you'll find it works fine,
and the 2; y; x+y; just doesn't do anything (that often
suprises people at first).

However, with this symbol and list, I suspect that writers
have overloaded the comma operator so that it can be used to
build lists. This is a nice trick which I've seen a few
times.

Actually, it's one of the best tricks I know when it comes to
obfuscation, and introducing subtle bugs. Consider the case
where x and y are variables in a larger scope, and you write:

List l1 ;
l1 = x, y ; // insert 2 elements into the list...
List l2 = x, y ; // define two lists, l2 and y...

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Cyril
Guest





PostPosted: Fri Jun 24, 2005 3:45 pm    Post subject: Re: How can this be working ? Reply with quote

Thanks for all your answers. I didn't know we could overload the comma
operator.

I guess I've read the standard carefully enough.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Fri Jun 24, 2005 5:43 pm    Post subject: Re: How can this be working ? Reply with quote

Ferdi Smit wrote:
Quote:
Now you can write code like this:
Vector<4, float> v = 1, 2, 3, 4;

No you can't. You've written a declaration, and the commas
in a declaration are not 'operator,()'. The code above won't
compile at all. You need to write it as
Vector<4, float> v;
v = 1, 2, 3, 4;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ferdi Smit
Guest





PostPosted: Sat Jun 25, 2005 1:22 am    Post subject: Re: How can this be working ? Reply with quote

"Hyman Rosen" <hyrosen (AT) mail (DOT) com> wrote

Quote:
Ferdi Smit wrote:
Now you can write code like this:
Vector<4, float> v = 1, 2, 3, 4;

No you can't. You've written a declaration, and the commas
in a declaration are not 'operator,()'. The code above won't
compile at all. You need to write it as
Vector<4, float> v;
v = 1, 2, 3, 4;

Yes sorry, you're right, I slipped up. I also reversed the order of '4' and
'float' btw in the example.


--
Regards,

Ferdi Smit
smit xs4all nl



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.