comp.lang.ada
 help / color / mirror / Atom feed
* Generic formal ">"
@ 2022-01-26 16:54 Simon Wright
  2022-01-26 18:46 ` Jeffrey R.Carter
  2022-01-26 18:48 ` AdaMagica
  0 siblings, 2 replies; 5+ messages in thread
From: Simon Wright @ 2022-01-26 16:54 UTC (permalink / raw)


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;

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-27 12:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-26 16:54 Generic formal ">" Simon Wright
2022-01-26 18:46 ` Jeffrey R.Carter
2022-01-26 18:48 ` AdaMagica
2022-01-26 19:00   ` AdaMagica
2022-01-27 12:05     ` Simon Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox