POV-Ray : Newsgroups : povray.off-topic : New NearInfinity demo up : Re: New NearInfinity demo up Server Time
6 Sep 2024 21:21:14 EDT (-0400)
  Re: New NearInfinity demo up  
From: Darren New
Date: 16 Dec 2008 15:43:10
Message: <494812de$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>>>   The temporary given as parameter is created only once, not 64 times.
> 
>> So the declaration is essentially hoisted from out of the loop? Makes sense.
> 
>   Ok, it may be possible that it is created in each loop, depending on how
> you are creating it, so I may have exaggerated that bit, and it may indeed
> be created 64 times.

I would think if the arguments are different each time, the constructor at 
least would have to be called, even if it's overwriting the previous memory 
locations. (I.e., if the compiler allocated the space once on the stack or 
even in static memory.) And if the compiler can't tell the class's 
destructor doesn't do something like calling 'destroy', it can't just 
overwrite the constructed value without destructing it first?

>   You could manually optimize it and create it only once outside the loops,
> to make sure. (If, for example, each sprite is otherwise identical but with
> different coordinates, you could simply change the coordinates and push it
> into the vector.)

Yep.

>   OTOH in this case it probably doesn't matter.

Yep.

>   Copy constructors are called, for example, when you pass an object to
> a function by value.

Good to know. Thanks!  Still wading thru the C++ manuals and tutorials here.

>   Actually it is perfectly possible to construct into an array element
> directly. (A programmer can do that himself by using so-called placement
> new.)

Sure. I was speaking of this particular bit of code, tho.

>   Whether or not the compiler is smart enough to optimize it like that
> is another question.

OK.

>> And 
>> wouldn't it have to call the destructor on the temporary 64 times after it 
>> was copied (assuming it has a destructor)?
> 
>   If the temporary is created 64 times, then it will be destroyed 64 times
> as well, of course.

OK. So you were assuming a trivial destructor? I'm just trying to learn.

>   As I said, you may be right in that the temporary is created 64 times,
> though. You could hand-optimize it by creating it once outside the loops.

I would bet that it's easier for the compiler to create the temporary only 
once than it is for the compiler to know it can call the constructor on the 
array elements in std::vector without any temporary needed at all. :-)

-- 
   Darren New, San Diego CA, USA (PST)
   The NFL should go international. I'd pay to
   see the Detroit Lions vs the Roman Catholics.


Post a reply to this message

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