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 02:46:08 PST Path: supernews.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!enews.sgi.com!proxad.net!feeder2.proxad.net!nnrp6.proxad.net.POSTED!not-for-mail Message-ID: <3AC46252.B7E54EA6@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> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Fri, 30 Mar 2001 10:41:38 GMT NNTP-Posting-Host: 158.140.208.29 X-Complaints-To: abuse@proxad.net X-Trace: nnrp6.proxad.net 985948898 158.140.208.29 (Fri, 30 Mar 2001 12:41:38 CEST) NNTP-Posting-Date: Fri, 30 Mar 2001 12:41:38 CEST Organization: Guest of ProXad - France Xref: supernews.google.com comp.lang.ada:6237 Date: 2001-03-30T10:41:38+00:00 List-Id: 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. 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; } > 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. -- Jean-Marc