From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a84eaf8fb2470909 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!n67g2000cwd.googlegroups.com!not-for-mail From: "Hyman Rosen" Newsgroups: comp.lang.ada Subject: Re: Ada generics Date: 21 Dec 2006 08:55:03 -0800 Organization: http://groups.google.com Message-ID: <1166720103.733870.120450@n67g2000cwd.googlegroups.com> References: <1166710494.869393.108730@a3g2000cwd.googlegroups.com> NNTP-Posting-Host: 204.253.248.208 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1166720110 25698 127.0.0.1 (21 Dec 2006 16:55:10 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 21 Dec 2006 16:55:10 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1; .NET CLR 2.0.50727) Gecko/20061204 Firefox/2.0.0.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: n67g2000cwd.googlegroups.com; posting-host=204.253.248.208; posting-account=lJDDWg0AAACmMd7wLM4osx8JUCDw_C_j Xref: g2news2.google.com comp.lang.ada:7974 Date: 2006-12-21T08:55:03-08:00 List-Id: markww wrote: > I'm trying to compare generics in Ada vs C++ templates There are fundamental differences between the two languages in how generics work that relate back to how types themselves work. In C++, a type is a completely static thing. You can read a type declaration in the code and know everything there is to know about it. In Ada, types are much more dynamic - they can include information known only at run type, such as array sizes and discriminant values. (I don't know Ada, so pardon me if I'm wrong in the Ada details.) Accordingly, C++ types don't have a lifetime as such, existing for the life of the program, but Ada types have the same lifetime as other objects in the scope in which they are declared. Among other things, this means that C++ types can be assigned unique names at compile time while Ada types cannot. Now, imagine you have a generic which wants to take a type parameter. In C++, because types are fixed and uniquely named, the generic itself can be instantiated at compile time. The compiler has all the information it needs. Furthermore, the instantiated generic can also be uniquely named. This becomes the paradigm of generic instantiation in C++ - to do it all in the compiler. Thus, generics may take non-type parameters as well, but they must all be constant (or refer to the address of a static object). As an additional consequence, C++ is able to identify instantiations with each other based on identity of the template parameters so that, for example, the type std::vector in one file is exactly the same type as std::vector in a second file. It also makes automatic instantiation of function templates easier, since again the full set of needed instantiations is known at compile time, and instantiations with the same set of parameters are identified with each other. For example, in a function template containing a static variable, there is one such variable generated for each different set of instantiation parameters. In Ada things are different. Because types have lifetimes and contain runtime information, generic instantiations must also. Furthermore, in Ada even functions and procedures have lifetimes, because they nest and have access to outer variables. Now it is not possible to know at compile time how many generics need to be instantiated, and you cannot identify a generic instantiation simply by looking at the generic and its parameters. Each instantiation is separate, and that makes automatic instantiation of generic functions problematic, because it may be difficult to know when to do a new instantiation and when to reuse an existing one. On the other hand, since you are already so dynamic, there doesn't need to be any restriction on non-type parameters, and so in Ada you can instantiate generics on locally-scoped functions and objects. Because(?) Ada requires explicit instantiations, it has no notion of generic specializations. This means that in Ada you cannot do metaprogramming the way you can in C++. In Ada a generic has one and only one implementation, whereas in C++ a template can be specialized for different sets of template parameters. On the other hand, Ada is much more friendly to generic sharing, that is, having a single or a few compiled functions that can act at runtime as particular instantiated functions.