Boost logo

Boost :

Subject: Re: [boost] safe integer library -- the scope
From: Robert Ramey (ramey_at_[hidden])
Date: 2015-12-11 23:22:37


On 12/11/15 6:47 PM, Oliver Kullmann wrote:
> Hello,
>
> On Fri, Dec 11, 2015 at 06:10:01PM -0800, Robert Ramey wrote:
>> On 12/11/15 5:50 PM, Robert Ramey wrote:
>> To belabor the point, consider this little program:
>>
>> #include <iostream>
>> #include <cstdint>
>> using namespace std;
>> int main(){
>> int8_t x = 100;
>> int y = x * x;
>> cout << y << endl;
>> uint32_t z1 = 100;
>> int8_t z2 = -100;
>> auto y2 = z1 * z2;
>> cout << y2 << endl;
>> }
>>
>> which prints out:
>>
>> 10000
>> 4294957296
>>
>> This is due to the application of the C++ type promotion rules.
>> Is it any reason that C++ drives people crazy?
>>
>
> This is the typical behaviour of C++ programmers, which are always
> hypercritical about the language, in stark contrast to any other
> programming language.
>
> The only problem I see above is that the compiler should issue a
> warning for the auto-line. And any reasonable programmer should not
> write such code.

This is actually the fundamental issue.

Shouldn't one be able to just look at an arithmetical statement and
expect it to produce a correct arithmetical result? Should one be
required to keep in mind that even though it looks like arithmetic and
it was designed to look like arithmetic on purpose there are a bunch of
extra-arithmetical rules (buried in the C++ standard) which one has to
keep in mind to verify that each statement is correct?

I think your point of view is typical of C/C++ programmers and in fact
of computer programmers in general. It's the view what we're not doing
math we're operating a machine. If that's our view we shouldn't even
use C++ expressions but rather stick to assembler where there is no
pretense that we're evaluating arithmetical expressions.

The reason programming languages were invented is so that we than think
in higher abstractions. In this case we want to think in terms of
algebra and arithmetic - and the way C++/C is implemented inhibiting us
from doing that. It's holding us back.

If it's any comfort, it seems that all computer languages - even
interpreted ones - suffer from this problem. It's amazing to me that 50
years of computer software evolution - and we're still living with this.

This is my main motivation for this effort.

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk