comp.lang.ada
 help / color / mirror / Atom feed
From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: Generic default parameters
Date: Mon, 13 May 2002 11:03:31 -0400
Date: 2002-05-13T11:03:31-04:00	[thread overview]
Message-ID: <3CDFD5C3.1070801@mail.com> (raw)
In-Reply-To: x7vadr7uj5d.fsf@pushface.org

Simon Wright wrote:
> I think what you're discussing here is related to "traits" in the C++
> world??? (something that's a black mystery to me). Or perhaps it's
> "partial specialization".

Let me try to explain what these are.
In C++, the equivalent of an Ada generic package is a class template.
In Ada, once you have a generic package, it is a fixed thing. You can
instantiate man packages from the generic, but they all share the same
specification and implementation code. In C++, you can write a partial
specialization, which causes an entirely different declaration and
implementation code to be used when the template is instantiated for
certain values of the generic parameters. (A refinement of this is the
explicit specialization, which is a specialization written for a single
set of generic parameter values.)

Here's a silly example of a compile-time factorial calculator:

// generic template
template<int N> struct factorial { enum { val = N * factorial<N-1>::val }; };
// specialization for N == 0
template<> struct factorial<0> { enum { val = 1 }; };
int f6 = factorial<6>::val;	// evaluates to 720 at compile-time

The compiler uses the generic template unless the template parameter
is zero, whereupon it uses the special case provided.

Then you can think of traits as the equivalent of Ada having user-defined
attributes. You write a class template that contains type definitions or
constants pertaining to a type, that you can't get from the compiler itself,
and then you write specializations for the types you need.

> Anyway, this would be very useful, I think, in simplifying a complex
> generic arrangement like the BCs'.

Yes, that's exactly the idea.




  reply	other threads:[~2002-05-13 15:03 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-10 14:22 Generic default parameters Thomas Wolf
2002-05-10 16:38 ` Preben Randhol
2002-05-10 16:50   ` Marin David Condic
2002-05-11  9:29     ` Simon Wright
2002-05-13 15:03       ` Hyman Rosen [this message]
2002-05-11 12:28     ` Preben Randhol
2002-05-13 14:03       ` Marin David Condic
2002-05-13 14:49       ` Hyman Rosen
2002-05-10 19:04   ` Hyman Rosen
2002-05-11 12:23     ` Preben Randhol
2002-05-11 13:49       ` Larry Kilgallen
2002-05-13 14:06       ` Marin David Condic
2002-05-10 19:27   ` Randy Brukardt
2002-05-11 12:32     ` Preben Randhol
2002-05-10 22:14 ` Stephen Leake
2002-05-13  7:49   ` Thomas Wolf
2002-05-13  8:49 ` Dmitry A. Kazakov
2002-05-13 14:00   ` Stephen Leake
2002-05-13 15:21     ` Dmitry A. Kazakov
2002-05-13 16:42       ` Stephen Leake
2002-05-14 10:24         ` Dmitry A. Kazakov
2002-05-14 14:02           ` Stephen Leake
2002-05-15 10:08             ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
2002-05-14  4:57 Grein, Christoph
2002-05-14 10:23 ` Dmitry A. Kazakov
2002-05-14 11:03 Grein, Christoph
2002-05-14 12:01 ` Dmitry A. Kazakov
2002-05-14 14:05   ` Stephen Leake
2002-05-15  8:44     ` Dmitry A. Kazakov
replies disabled

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