Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5195

Re: advice -- temp var for constructors?

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: advice -- temp var for constructors?
Date 2011-05-20 07:16 +0200
Organization A noiseless patient Spider
Message-ID <ir4t4h$aah$1@dont-email.me> (permalink)
References <buo1uzuxa3i.fsf@dhlpc061.dev.necel.com>

Show all headers | View raw


* Miles Bader, on 20.05.2011 06:35:
> Inside a class constructor, I often wish I could use temporary
> intermediate while calculating the initialization values -- both for
> clarity and efficiency -- but there doesn't seem to be any reallly good
> way to do this.
>
> E.g., consider a class A with fields x, y, z.  I want to initialize x,
> y, and z from a constructor argument "arg".  If I was writing normal
> code, I could do something like:
>
>     int temp1 = some_long_calculation (arg);
>     int temp2 = some_other_long_calculation (arg);
>     x = blah_blah (temp1, temp2);
>     y = oink_oink (temp1, temp2);
>     z = zergh_zergh (temp1, temp2);
>
> but inside constructor initialization forms, one can't create "temp1"
> and "temp2".
>
> Soooo, possible workarounds:
>
> (1) Don't use initialization forms at all (at least for data members
>      that use a temp variable), but instead just write code like the
>      above in the constructor body.
>
>      [This often works, but I think would generally be considered kind of
>      ugly, and in some cases is less efficient.]
>
> (2) Add private class data members for each temporary variable, and only
>      actually use them during the initialization.
>
>      [This is also ugly, wastes space in each object, and in some cases
>      using data members as temporaries seems to result in less efficient
>      code than real temporary variables (maybe because alias analysis is
>      more difficult).]
>
> (3) Just bite the bullet, and don't use temporary variables at all --
>      instead duplicate the expressions and hope the compiler can detect
>      the duplication and eliminate the redundancy itself.
>
>      [Less readable, often less efficient, less maintainable because of
>      the duplication.]
>
> None of these solutions seems very satisfying.  Does anybody have advice
> about other, better, methods...?

E.g. like (off the cuff, not touched by compilers' dirty hands)


<code>
class Foo
{
private:
     struct State
     {
         int x, y, z;

         State( int t1, int t2 )
             : x( blah_blah( t1, t2 ) )
             , y( oink_oink( t1, t2 ) )
             , z( zergh_zergh( t1, t2 ) )
         {}
     } state;

public:
     // Whatever, then

     Foo( int arg )
         : state( some_calc_1( arg ), some_long_calc_2( arg ) )
     {}
};
</code>


Cheers & hth.,

- Alf "let me see that for you - lmstfy.com"

-- 
blog at <url: http://alfps.wordpress.com>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

advice -- temp var for constructors? Miles Bader <miles@gnu.org> - 2011-05-20 13:35 +0900
  Re: advice -- temp var for constructors? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-20 07:16 +0200
  Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 10:38 +0200
    Re: advice -- temp var for constructors? Miles Bader <miles@gnu.org> - 2011-05-20 17:50 +0900
      Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 11:12 +0200
  Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 10:23 +0000
    Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 13:02 +0200
      Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 13:55 +0000
        Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 14:28 +0000

csiph-web