Swaping?

T

Tomás Ó hÉilidhe

James Kanze:
In that case, of course: if such analysis turns you on, then by
all means, do it. Just realize that that isn't the case for
most people programming in C++ (or just programming, for that
matter---but C++'s virtues really come to the fore in big
projects, which of course, an amateur can't do).


(Just to clarify before the discussion develops further: I use "amateur" in
contrast to "professional" to denote someone who doesn't get paid for what
they're doing. It's not any indication of the programmer's competance.)

I think you're overlooking the whole free software movement. Think Linux,
OpenOffice, Mozilla. Plenty of competant amateur programmers working on a
big project.

Quite. It wouldn't be fun otherwise, would it?


Nope.
 
J

jkherciueh

Tomás Ó hÉilidhe said:
James Kanze:



(Just to clarify before the discussion develops further: I use "amateur"
in contrast to "professional" to denote someone who doesn't get paid for
what they're doing. It's not any indication of the programmer's
competance.)

I think you're overlooking the whole free software movement. Think Linux,
OpenOffice, Mozilla. Plenty of competant amateur programmers working on a
big project.

Actually, the main difference is neither in getting paid nor in skill. The
important difference in the context of premature optimization and cryptic
coding is whether you are subject to some coding standard to facilitate
teamwork and maintainability. In none of the projects you listed, a
programmer would get away with cryptic code based on nothing more than
personal preference and a conjectured marginal performance gain.

Besides, I would venture the guess that James will not consider the projects
you mentioned as big :)

Also: That the code is free doesn't imply that programmers don't get paid
for it. If I recall correctly, IBM has contributed code to Linux; and I bet
you the programmers didn't write it in their free time.

[snip]


Best

Kai-Uwe Bux
 
T

tryptik

Actually that's a bad example; Windows XP wouldn't run at all if
they went for the easist solution every time because Microsoft
programmers are mostly incompetant.

Just for the record, I am pretty sure MS programmers are good - it is
the manangement and the cosntraints they work under that make for bad
products.

As for swapping, I am pretty sure std::swap makes use of type traits
to select the best swapping algorithm for the object in question.
 
J

Jerry Coffin

[ ... ]
As for swapping, I am pretty sure std::swap makes use of type traits
to select the best swapping algorithm for the object in question.

Mostly it uses specialization to do the job. Quite a few things in the
standard library (e.g. containers) are required to provide
specializations of swap. You're also allowed to add a specialization of
swap to namespace std for your own types, when/if that makes sense.
 
J

jkherciueh

Jerry said:
[ ... ]
As for swapping, I am pretty sure std::swap makes use of type traits
to select the best swapping algorithm for the object in question.

Mostly it uses specialization to do the job. Quite a few things in the
standard library (e.g. containers) are required to provide
specializations of swap.

Actually, those are overloads.
You're also allowed to add a specialization of
swap to namespace std for your own types, when/if that makes sense.

Unfortunately, the issue is a little more complicated (due to lack of
precision and foresight in the standard).

a) As you correctly point out, one is allowed to add _specializations_.
However, one is not allowed to add _overloads_ (at least formally the
standard does not give that license).

Consequently, the correct way to insert a custom swap for your own type
MyType into namespace standard looks suprisingly complicated:

namespace std {

template<>
void swap( MyType & lhs, MyType & rhs ) {
lhs.swap( rhs );
}

}


b) Adding insult to injury, it becomes therefore impossible to provide a
custom swap in namespace std for your own templates. If you do

namespace std {

template < typename T >
void swap ( MyTemplate<T> & lhs, MyTemplate<T> & rhs ) {
lhs.swap( rhs );
}

}

you are just adding overloads. The language does not (yet?) allow for
partial specializations of function templates.


c) When writing your own generic algorithms (which reside outside namespace
std), you have to decide whether to use

std::swap( a, b );

or

swap( a, b ); // use dependent name lookup to find the right swap.

It is unspecified which version the standard library uses, so there is no
clear lead to follow. (Maybe, it is good idea to do

namespace my_stuff {

using std::swap;

...

void some_function ( ... ) {
...
swap( a, b );
...
}

}

which would hopefully use std::swap unless dependent name lookup provides
the better match.)


d) The next version of the standard introduces the swappable concept. To be
swappable, the expression

swap( a, b )

is required to work. Then the algorithms in namespace std will use swap(a,b)
instead of std::swap(a,b) and putting the swap function into the same
namespace as the class / template (relying on dependent name lookup) seems
to be the RightThing(tm) with C++0X.


As of now, making sure that your custom swap will be found no matter what
can become quite a hassle.



Best

Kai-Uwe Bux
 
J

James Kanze

James Kanze:
(Just to clarify before the discussion develops further: I use
"amateur" in contrast to "professional" to denote someone who
doesn't get paid for what they're doing. It's not any
indication of the programmer's competance.)

Agreed. What it does affect is his motivation for certain
decisions.
I think you're overlooking the whole free software movement.
Think Linux, OpenOffice, Mozilla. Plenty of competant amateur
programmers working on a big project.

Well, most of them are professionals, paid by someone to work on
the project. But even those who aren't:

An amateur does things for the love of it, or for pleasure (or
only for pleasure---a professional can take pleasure in his work
as well). But what causes pleasure depends---for some, I'm
sure, it is the sentiment of having done something useful, or
the feeling that what he has done is being used. Or the pride
of having participated in creating something of quality. (I
know that those are important aspects for me, in addition to the
money.) Given that, one might be willing to do some things
which don't provide pleasure directly, in themselves, because
they contribute to the final goal, which does provide pleasure.
Which means that they may decide to write documentation
(although they don't like writing documentation) rather than
gaining a couple of microseconds in a function which is called
once an hour (although such "tuning" can be quite amusing).

The analogy isn't really exact, but think of a musician playing
scales. Playing scales, in itself, certainly isn't a direct
source of pleasure. But the improved ability which comes from
it is.
 
J

James Kanze

Actually, the main difference is neither in getting paid nor
in skill. The important difference in the context of premature
optimization and cryptic coding is whether you are subject to
some coding standard to facilitate teamwork and
maintainability. In none of the projects you listed, a
programmer would get away with cryptic code based on nothing
more than personal preference and a conjectured marginal
performance gain.

Perhaps not in those particular projects (although I don't
know---the only one I'm sure of is g++). But I could certainly
get away with it in my own library, and I don't.

In a certain way, you could argue that everyone is getting
paid---if not in money, then in some form of pleasure or
satisfaction. I've done the type of optimizations hÉilidhe
mentions in the past---I enjoy doing them. But I enjoy even
more the feeling I get from a job well done, or from people
admiring my code and finding it well written and maintainable,
or learning from it. When I code my library, I work for the
"greatest pleasure" (which is the greatest return on the
investment of my time). My greatest pleasure, but my greatest
pleasure comes from the viceral feeling of having produced
something of quality that is useful. So I forego the smaller
pleasure of tricky coding.

But that's me, and that's what turns me on.
Besides, I would venture the guess that James will not
consider the projects you mentioned as big :)

It depends. Apache is certainly big---bigger than the servers I
work on, in fact. (Customizability creates size.)
Also: That the code is free doesn't imply that programmers
don't get paid for it. If I recall correctly, IBM has
contributed code to Linux; and I bet you the programmers
didn't write it in their free time.

And that those that did did so for the pleasure of creating a
quality product that people would use.
 
T

Tomás Ó hÉilidhe

(e-mail address removed):
Just for the record, I am pretty sure MS programmers are good - it is
the manangement and the cosntraints they work under that make for bad
products.


If I go a week without going to www.windowsupdate.com, then I can be
sure I'll get two or three "high priority" updates whose description is
something like "A vulnerability has been identified in <whatever> that may
allow an intruder to gain access to your computer".

When playing around with networking on Windows machines, I always sever
the internet connection before dropping the firewall... and that's he when
I *shouldn't* have any daemons running. Remember that "MSBlaster" virus.
You didn't have to do a single thing in order for it to damage your
computer. You literally just sat there reading your newsgroups and all of a
sudden your system tells you you've 30 seconds before it shuts down.
 
R

Rolf Magnus

Pete said:
But the comment is wrong. First, the code doesn't swap the values of
two integer types, it swaps the values of two integers. Second, it
doesn't always swap the values of two integers,

It does.
as has been pointed out elsewhere.

If you are pedantic, you should also be consequent about it. What I see
pointed out is that if a and b are references to the very same integer, it
doesn't work. You can't use it to swap a single integer with itself.
Swapping two integers should work fine.
 
P

Pete Becker

It does.


If you are pedantic, you should also be consequent about it. What I see
pointed out is that if a and b are references to the very same integer, it
doesn't work. You can't use it to swap a single integer with itself.
Swapping two integers should work fine.

Documentation that relies on such subtle word play would be a
disservice to its users.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top