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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,571930b4ff0bc1ee X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-30 08:52:06 PST Path: supernews.google.com!sn-xit-03!supernews.com!freenix!proxad.net!feeder2.proxad.net!nnrp3.proxad.net.POSTED!not-for-mail Message-ID: <3AC4B7F9.7C73455A@free.fr> From: Jean-Marc Bourguet X-Mailer: Mozilla 4.76 [en] (X11; U; SunOS 5.7 sun4u) X-Accept-Language: en, French, fr MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Compile time executed functions References: <3AC03CCE.70E3C2D5@mida.se> <3AC18DD1.EF25CE42@mida.se> <5mzw6.415$1H6.72722473@newssvr16.news.prodigy.com> <3AC2EB17.33AAEC0A@mida.se> <3AC46252.B7E54EA6@free.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Fri, 30 Mar 2001 16:47:03 GMT NNTP-Posting-Host: 158.140.208.29 X-Complaints-To: abuse@proxad.net X-Trace: nnrp3.proxad.net 985970823 158.140.208.29 (Fri, 30 Mar 2001 18:47:03 CEST) NNTP-Posting-Date: Fri, 30 Mar 2001 18:47:03 CEST Organization: Guest of ProXad - France Xref: supernews.google.com comp.lang.ada:6254 Date: 2001-03-30T16:47:03+00:00 List-Id: Ken Garlington wrote: > > "Jean-Marc Bourguet" wrote in message > news:3AC46252.B7E54EA6@free.fr... > : Ken Garlington wrote: > : > : > Could you post an example of such a template, and what in the C++ > standard > : > you rely upon to guarantee compile-time calculations? > : > : First feature, things like > : enum { foo = x+y*z; } > : are valid as long as x, y and z value are known at compile time and have > : to be evaluated at compile time. > > Isn't this usually resolvable as a constant by most Ada compilers? At least > the ones I know will usually do so. It is quite difficult to come with the equivalent in Ada. One is foo : constant = x+y*z; is indeed resolvable by most Ada compiler. > More to the point, does the C++ standard _require_ that foo not generate any > assembly instructions to be executed at run-time? If so, what does it say? I've not checked what the standard says precisely. The enum values are a compile time constant. They may be used in context like case label which are to be fixed at compile time. This enum { a = 2, b = 3, c = 4, d = 7 }; enum { foo = a+b*c }; enum { bar = a*d }; void f() { int foobar; switch(foobar) { case foo: break; case bar: break; } } is required to produce a diagnostic as foo and bar have the same value. > : Second feature it is possible to provide "specialization" for template, > : that is provide a different implementation when some template parameter > : have some constant value. > : > : The first ensure that evaluation is made at compile time. The second > : that it is possible to make some test. Looping is available with > : recursion so finally you have a computationnally complete language at > : compile time. It is painfull to write and even more painfull to use but > : some like abusing the language... > : > : An example, how to compute factorial > : > : #include > : > : template > : struct factorial { > : enum { RET = factorial::RET*F }; > : }; > : > : template <> > : struct factorial<1> { > : enum { RET = 1 }; > : }; > : > : int main() { > : std::cout << factorial<10>::RET << std::endl; > : } > > Are you claiming that this resolves to a single constant value prior to > execution? Yes. factorial<10>::RET is a single constant value. > I don't see how... factorial<10>::RET is defined to be factorial<9>::RET*10 ... factorial<2>::RET is defined to be factorial<1>::RET*2 factorial<1>::RET is defined to be 1 by the specialization (template<> ...) using template as compile time language is very functionnal. > : > Also, does > : > "compile-time" include "link-time", or are you not allowed to have > anything > : > in the table that uses addresses? > : > : As some C++ compilers do template instanciation at "link-time", I think > : the answer is yes. > > I don't think we're talking about the same thing here... I don't think your are allowed to do pointer arithmetic in this context. Does that answer better your question? -- Jean-Marc BTW, template in C++ is so complex and this kind of thing push the limit so much that you have to code taking into account the compiler bugs and limitation. So such code is very difficult to port.