Generic formal ">"
From
Simon Wright@21:1/5 to
All on Wed Jan 26 16:54:50 2022
I have a generic with a formal private type Element_Type and a formal
function ">" (L, R : Element_Type) return Boolean. This gives me
puzzling and compiler-dependent behaviour when I instantiate with
">" => "<".
The output from the code below is
GCC 10.1.0:
Data'Length: 2
Data'Length > 1: TRUE
Integer'(Data'Length) > 1: TRUE
Standard.">" (Data'Length, 1): TRUE
Standard.">" (Integer'(Data'Length), 1): TRUE
GCC 11.1.0:
Data'Length: 2
Data'Length > 1: FALSE
Integer'(Data'Length) > 1: TRUE
Standard.">" (Data'Length, 1): FALSE
Standard.">" (Integer'(Data'Length), 1): TRUE
I don't know what version of ">" will be used when the unqualified
Data'Length is involved, but I would have expected that the overridden
("<") version would be used in the Integer'(Data'Length)
version. (Sorry, I don't think overridden is the right word).
GCC 10 uses the version from package standard even when given
integer-qualified data.
GCC 11 uses the version from package standard when given
integer-qualified data, and the overriding version when not.
Is it right that Standard.">" is getting overridden?
Have the rules changed in this area?
My "fix" was to compare Data'Length >= 2!
8X-------------------
with Ada.Text_IO; use Ada.Text_IO;
procedure Gen_Cmp is
generic
type Element_Type is private;
type Index_Type is (<>);
type Array_Type is array (Index_Type range <>) of Element_Type;
with function ">" (Left, Right : Element_Type) return Boolean is <>;
procedure Gen (Data: in out Array_Type);
procedure Gen (Data: in out Array_Type) is
begin
Put_Line ("Data'Length:" & Data'Length'Image);
Put_Line ("Data'Length > 1: "
& Boolean'Image (Data'Length > 1));
Put_Line ("Integer'(Data'Length) > 1: "
& Boolean'Image (Integer'(Data'Length) > 1));
Put_Line ("Standard."">"" (Data'Length, 1): "
& Boolean'Image (Standard.">" (Data'Length, 1)));
Put_Line ("Standard."">"" (Integer'(Data'Length), 1): "
& Boolean'Image (Standard.">" (Integer'(Data'Length), 1)));
end Gen;
type Alpha is
(A, B, C, D, E, F, G, H, I, J, K, L, M,
N, O, P, Q, R, S, T, U, V, W, X, Y, Z);
type My_Array is array (Alpha range <>) of Integer;
procedure Chk_Down is new Gen (Element_Type => Integer,
Index_Type => Alpha,
Array_Type => My_Array,
">" => "<");
Data : My_Array (A .. B);
begin
Chk_Down (Data);
end Gen_Cmp;
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)