POV-Ray : Newsgroups : povray.off-topic : A question about Java generics (not a flame) : Re: A question about Java generics (not a flame) Server Time
7 Sep 2024 23:25:36 EDT (-0400)
  Re: A question about Java generics (not a flame)  
From: Warp
Date: 13 May 2008 05:56:34
Message: <482965d2@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   But it's compiled only once even if the same template function is used
> > with a given type at several places in the program. (Without the export
> > template mechanism the function would be compiled every time it's used.)

> Even if the places it's used are in different compilation units?

  Yes. The compiler couldn't compile the template function even if it wanted
because it only has its declaration, not the implementation (which is in
its own compilation unit).

> As in, 
> if I invoke the compiler 12 times with 12 instantiations of the same 
> template in 12 different .cpp files, isn't that code going to have to 
> get translated into object code 12 different times?

  We are talking about export templates here, not regular ones.

> To phrase it differently, say it took one minute to compile the program 
> with the template inline. Could I compile 12 compilation units that 
> instantiate the template in 2 minutes? Or is it going to take on the 
> order of 12 minutes?

  If the function is an export template, it will only be compiled once,
not 12 times. (Well, naturally assuming all those 12 times use the same
type with that template.)

> >   And, as I said, I can imagine that compilers could implement optimizations
> > where they cache compiled export templates. IOW, if you compile your project
> > for the first time, the template code gets compiled, but if you recompile
> > the project a second time, the already-compiled template code is used.
> > (But I doubt any C++ compiler so far uses this kind of optimization.)

>  From the link Fredrik posted, it looks like you can't really do this, 
> because the environment that invokes the instantiation may be different. 
> (E.g., there may be different overloaded functions in scope that would 
> change the meaning of the template.)

  The environment doesn't change with export templates because they are
in their own compilation units. The only environment is that compilation
unit in question. They are basically no different from regular functions.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.