P
Peter Koch Larsen
Tapio Kelloniemi said:Yes, but I can tell the compiler that I know better:
V : Velocity := Velocity (Integer (M) / Integer (S));
You can't be serious here. You just did what corresponds to a C-style cast
here. Would anyone accept that in production-code? The other proposal (to
create a function) simply requires a vast amount of stupid code if all
"normal" generic dimensions.
Also other readers of my code now know surely that this is what I meant.
So they say, the ones who abuses C-style casts.
Not so. There is an excellent library which does exactly what you want -
using templates, of course.
That is a good thing. If all C++ programmers used it, I think their
programs would benefit a lot of it.
What makes you believe they happen regularly in C++?
They just happen. Why people had otherwise created such tools as valgrind
(http://valgrind.kde.org/). I also use it myself for Ada to eliminate
memory
leaks. Writing past array bounds unintentionally is quite easy. In Ada
Constraint_Error is raised, but C++ program segfaults. Or even worse, it
does not segfault immediately, but when the function is exited (as the
return address has been destroyed). STL is safer, but it cannot always be
used (eg. when interfacing to foreign code).
An example. First compile with the default behaviour, then with all
warnings tu
rned on: [--]
If a replace I < J; with null; the result is:
gcc -c -gnatg temp.adb
temp.adb:2:04: warning: "I" is not modified, could be declared constant
temp.adb:2:04: warning: variable "I" is not referenced
temp.adb:3:04: warning: "J" is not modified, could be declared constant
temp.adb:3:04: warning: variable "J" is not referenced
gnatmake: "temp.adb" compilation error
I see no real difference here between a good-quality C++ compiler and Ada.
Another example:
int* f()
{
int i = 3;
int& l = *(new int);
l = i;
int* p = &i; // Should be int* p = &l;
return p;
}
int main()
{
int* p = f();
*p = 4;
return 0;
}
# g++ -Wall -o temp temp.cc
#
Not even a warning and the program does not crash on my system! Valgrind
revealed the error (and thought it was a G++ bug).
If the code above is modified a bit, even the best compiler cannot
know for sure that we are doing evil things.
Noone denies that writing code such as this is possible in C++. Apparantly
you can also do it in Ada. In neither language will you see such stuff in
production-code. This at least is my sincere hope ;-)
The same in Ada:
procedure Temp is
type Int_Ptr is access Integer;
function F return Int_Ptr is
I : Integer := 3;
L : Int_Ptr := new Integer;
P : Int_Ptr;
begin
L.all := I;
P := I'Access; -- Should be P := L;
return P;
end F;
A : Int_Ptr;
begin
A := F;
A.all := 4;
end Temp;
# gcc -c -gnatg temp.adb
temp.adb:5:04: (style): subprogram body has no previous spec
temp.adb:11:12: prefix of "Access" attribute must be aliased
So if I really want to pass a pointer to a local to the caller, I should
change
I : Integer := 3;
to
I : aliased Integer := 3;
Also note that the lines which were "mistyped" are much closer to each
other in C++ than in Ada.
/Peter