Grizlyk said:
Gavin Deane wrote:
1.
I am speaking exactly about C++ exception - "The goal of C++ exception
mechanism" - not about all other types of "exceptions" in system.
Good. So why do you continue to bring up the concept of divide by zero,
which has no relation to C++ exceptions?
2.
">there is no way of knowing what code that tries to divide by zero
will do"
a) the expression is not opposite to my conclusion - "error code often
must post the error to up level"
If you think that "opposite" ( that "error code must _not_ post the
error to up level"), then you just disagree with exception's goal due
to "overhead" or something else.
I agree that errors *detected by the program* often want to be handled
at a higher level than they are detected and that C++ exceptons are a
mechanism for doing that. However, that has nothing whatsoever to do
with divide by zero.
b) any C++ exception condition can be resolved manualy by programmer
like this:
if (error_condition) throw error_class();
Agreed.
c) in the case of divide_by_zero, expression "if (error_condition) " is
_hardware_ supported,
It might be or it might not. That is up to the hardware designer and is
most certainly outside the scope of the C++ language.
c-1) you can not test condition befor operation in program manualy
What??? You might not be able to think of a way, but I can.
if (rhs != 0) // test the condition before the operation manually
lhs/rhs;
else
// do something else
c-2) you can not deny the _hardware_ test
Assuming the hardware test exists, I agree there won't be a standard
C++ means to deny it.
c-2) the _hardware_ test gives no "overhead" if no error condition
occured
That's up to the CPU designer and outside the scope of C++.
But C++ does not garantee, that after _hardware_ "if (error_condition)"
passed, programmer can write "throw error_class()" instead of "exit()".
Absolutely not, because C++ has no way of knowing what might happen.
The possibilities are infinte.
c-3) In order to do the "garantee", C++ must convert system depended
error into system independent exception with the help of std lib, for
example.
I contend that that is impossible, because the set of system dependent
behaviours that might occur is, as far as C++ is concerned, infinite.
Not because an infinite number of different CPU behaviours actually
exisits in the real world, but because for C++ to assume only a finite
set of CPU behaviours it will support would mean, by definition, that
some of the infinite set is left out, and C++ has no right to do that.
c-4) Ian Collins wrote befor, that even C does not garantee signals,
that was not risen by "raise()" in program. He thinks, that on some
abstract systems that could not support some properties, the
implementation of the std behaviour could be hard to do.
It is not true.
If you disagree with Ian Collins, take it up with him if you haven't
done so already.
c-4-1) Remember CPU without DIV and MUL opcodes. To implement
operations "/" and "*" C/C++ compilers must call internal functions to
do the actions. No one say, that it is "overhead". It is problem of
concrete CPU, that can not do std operation directly.
Absolutely. And that provides a nice example for an analogy.
Here is what appears to be going on:
Grizlyk: I want defined behaviour if my C++ program divides by zero
with built in arithmentic types.
Me (and others): You can't have that because the behaviour of the
system and operating environment in the event of divide by zero is not
known. Every system and environment could be different, and new ones
could come along with their own new behaviour, and one of the goals of
C++ is that it can be used to program all of them.
Grizlyk: But some systems behave in a particular way so if C++ required
that behaviour, we could have defined behaviour of the program on
divide by zero.
Me (and others): That may be true, but then you could no longer use C++
to program any system that behaved differently and that would go
against the goals of C++.
Which is analogous to:
Grizlyk (hypothetically): I want division of build in arithmetic types
to be a single machine instruction.
Me (hypothetically): You can't have that because the means used by the
system to carry out division is not known. Every system and environment
could be different, and new ones could come along with their own new
behaviour, and one of the goals of C++ is that it can be used to
program all of them.
Grizlyk (hypothetically): But some systems can do division in a single
instruction so if C++ required that behaviour, we could have what I
want.
Me (hypothetically): That may be true, but then you could no longer use
C++ to program any system that behaved differently and that would go
against the goals of C++.
c-4-2) When we are doing ordinary arithmetic operation as "divide"
and program fall into "undefinite behaviour" due to C++ fighting
against "overhead" it is wrong arithmetic operation behaviour. Wrong
because
a) programmer can not test error condition befor operation ( due to
system-depended _hardware_ test )
Here you go again!!! How is the following not testing the error
condition before the operation?
if (rhs != 0) // test the condition before the operation manually
lhs/rhs;
else
// do something else
b) programmer can not post error to up level if _hardware_ error
occured ( due to system-depended _hardware_ form of error message )
True. But irrelevant, because, despite your assertions to the contrary,
the programmer can very easily detect the error condition *before* the
operation and handle it as required at that time. No need to carry out
the operation which means no need to encounter system dependent
behaviour.
The trivial operations, as arithmetic operations, must be _reliable_,
Why? C++ and C (C++ inherited this issue from C) do not define
behaviour on divide by zero, which you seem to think is "unreliable".
And yet there is a world of C and C++ programmers who have been writing
successful programs with this "unreliability" for decades.
and C++ must assume, that CPU and OS will support restore wothout
"overhead" after such the trivial reliable operations, as arithmetic
operations. Otherwise all "overhead" is concrete CPU problem, as absent
DIV and MUL opcodes.
By what authority may C++ assume such behaviour of the CPU and OS?
Every time C++ assumes some underlying system behaviour, C++ becomes
unusable on all systems that behave differently. That entirely
contradicts the goal of C++ to be implementable on all systems.
To support trivial operations C++ or
a) must automatic call throw of std::div_by_zero_error and all other
overflows (+,-,*),
b) silently must ignore overflow, as for "+" operation,
c) introduce std error flag: a/=b; if( std::divide_by_zero() )throw
user_class();
To halt program due to overflow on division is wrong C++ behavior.
What would you rather have happen? An attempt to divide by zero is
almost certainly a bug. I am very keen on programs halting and falling
over very obviously when a bug in encountered.
">There is certainly no reason to assume that a C++ exception will be
thrown."
You can not prove you opinion with the help of standard, while you are
considering behaviour of standard.
In the abstract, which is the relevant context when, as in this case,
the particular platform has not been specified, the language standard
is the only tool available with which to reason about the behaviour of
a program. In this case the language standard is quite clear. The
behaviour on divide by zero is undefined, and that standard tells you
what "undefined" means. It means you have stepped outside the scope of
the standard. So, still in the abstract, it is no longer possible to
infer *anything* about the behaviour of the program, which means that
both the following statements are not opinion, they are fact:
There is certainly no reason to assume that a C++ exception will be
thrown.
There is certainly no reason to assume that a C++ exception will not be
thrown.
Because such the trivial operations, as arithmetic operations, must be
_reliable_.
*WHY* must they be "reliable"? Just because you want it that way? You
snipped the part of my previous post where I pointed out that you are
free to propose changes to C++ to the standards committee. I'll remind
you again of the two points I made:
1. Your proposal will change the way arithmetic operations on built in
types works. The change must imply zero overhead. Code that does
arithemtic on built in types must run just as quickly and use no more
memory on a compiler that implements you proposal than such code does
on today's compilers. If you don't like that, you need a different
language. C++ is probably not for you.
2. You have already seen in this thread the basics of a wrapper
template that can give *you* all the reliability you desire, while
leaving *me* perfectly free to use the "unreliable" built in types when
I don't need the protection of the wrapper. You could have that class
written, unit-tested and documented in a day and you would be able to
use it for ever after, and no change to the C++ language is required.
So if you are going to propose a change to the committee, I would
suggest you prepare an argument as to why your change is needed. That's
just my opinion as I have no connection to the committee. But if I were
on the committee, one of the first questions I would ask you would be
"You can implement arithmetic types that provide all the reliability
you want very simply as C++ stands today. Given that, why do you think
the langauge should change?".
Gavin Deane