comp.lang.ada
 help / color / mirror / Atom feed
* generic function and overloading
@ 2007-10-18  7:28 eliben
  2007-10-18  8:18 ` Jean-Pierre Rosen
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: eliben @ 2007-10-18  7:28 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="iso-8859-8-i", Size: 997 bytes --]

Hello,

I have a few "hardware" types, for example uint16 and uint32. And I
want to write a functions that will set or clear bits of such types.
For example:

function Bit_Set(word: uint16; bitn: natural) return uint16;

function Bit_Set(word: uint16; bitn: natural) return uint16 is
  mask: uint16 := 2**bitn;
begin
  return word or mask;
end Bit_Set;

However, I realize that such a function would be almost completely
duplicated for the uint32 type. In C++ I would probably define it as a
template on the type of the word, and the compiler would do the job
for me. In Ada, however, when using generics it doesn't seem I can
leave the same function name for all types (as I would do in
overloading). I can define a generic Bit_Set, but then I have to
specialize it for uin16 and uint32 with different function names. Is
there any way I could combine the effects of generics and overloading
and get a single Bit_Set for all my types without writing the code N
times ?

P.S. Ada95

Thanks,
Eli




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

end of thread, other threads:[~2007-10-19 13:59 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-18  7:28 generic function and overloading eliben
2007-10-18  8:18 ` Jean-Pierre Rosen
2007-10-18  9:31 ` Dmitry A. Kazakov
2007-10-18  9:33   ` Dmitry A. Kazakov
2007-10-18 10:32 ` Georg Bauhaus
2007-10-18 11:07 ` Stephen Leake
2007-10-18 11:30 ` Stefan Lucks
2007-10-18 15:58 ` Robert A Duff
2007-10-18 20:20   ` Simon Wright
2007-10-18 21:23     ` Robert A Duff
2007-10-19  5:19       ` Simon Wright
2007-10-19 13:59         ` Robert A Duff
2007-10-18 18:58 ` Dr. Adrian Wrigley
2007-10-18 22:30   ` Adam Beneschan

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