October 04, 2007
On Thu, 04 Oct 2007 17:55:05 +0900, Bill Baxter wrote:

> I find it hard to believe that a compiler couldn't recognize that this
> is a swap operation:
>     tmp = a;
>     a = b;
>     b = tmp;
> 
> If it's not harder than I think for some reason, then it's not really needed in the language.

Maybe it could, but that's not the point of a programming language. A programming language is for people, not computers, to read. It is whole lot easy to recognise "swap(a,b)" or even "a<->b" as a swapping operation than three lines of code.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
October 04, 2007
Derek Parnell a écrit :
> On Thu, 04 Oct 2007 17:55:05 +0900, Bill Baxter wrote:
> 
>> I find it hard to believe that a compiler couldn't recognize that this is a swap operation:
>>     tmp = a;
>>     a = b;
>>     b = tmp;
>>
>> If it's not harder than I think for some reason, then it's not really needed in the language.
> 
> Maybe it could, but that's not the point of a programming language. A
> programming language is for people, not computers, to read. It is whole lot
> easy to recognise "swap(a,b)" or even "a<->b" as a swapping operation than
> three lines of code.

Uh? If 'swap(a,b)' is ok, then you put swap as a function call, that the compiler inline, or as a macro if you want to be sure that it will be inlined.

As for an operator, given that I don't remember the last time that I needed to swap variables, I would vote against it.

renoX


October 04, 2007
Derek Parnell wrote:
> On Thu, 04 Oct 2007 17:55:05 +0900, Bill Baxter wrote:
> 
>> I find it hard to believe that a compiler couldn't recognize that this is a swap operation:
>>     tmp = a;
>>     a = b;
>>     b = tmp;
>>
>> If it's not harder than I think for some reason, then it's not really needed in the language.
> 
> Maybe it could, but that's not the point of a programming language. A
> programming language is for people, not computers, to read. It is whole lot
> easy to recognise "swap(a,b)" or even "a<->b" as a swapping operation than
> three lines of code.

Right, so you put those three lines in a template function called "swap".  If the compiler can recognize that's exchanging the values of a and b, then there's no need for swap to be a compiler intrinsic.  The plain old function is good enough.

--bb
1 2
Next ›   Last »