advice -- temp var for constructors?

Discussion in 'C++' started by Miles Bader, May 20, 2011.

  1. Miles Bader

    Miles Bader Guest

    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...?

    Thanks,

    -Miles

    --
    Arrest, v. Formally to detain one accused of unusualness.
    Miles Bader, May 20, 2011
    #1
    1. Advertising

  2. * 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>
    Alf P. Steinbach /Usenet, May 20, 2011
    #2
    1. Advertising

  3. Miles Bader

    Balog Pal Guest

    "Miles Bader" <>
    > 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.


    Hm? Call a function that returns what you want, calculated any complex way
    you like... You can make it a private static in the class. You can even make
    it non-static, but then be extremely careful to use only members that are
    already constucted, and spam comments in the class to avoid reordering.
    Balog Pal, May 20, 2011
    #3
  4. Miles Bader

    Miles Bader Guest

    "Balog Pal" <> writes:
    >> 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.

    >
    > Hm? Call a function that returns what you want, calculated any complex
    > way you like... You can make it a private static in the class. You can
    > even make it non-static, but then be extremely careful to use only
    > members that are already constucted, and spam comments in the class to
    > avoid reordering.


    The problem with that is that multiple data members are initialized from
    the _same_ set of temporaries (maybe I should have stated this more
    explicitly) -- if it's only a single data member, I do indeed just use a
    function, and that of course works fine.

    -Miles

    --
    Future, n. That period of time in which our affairs prosper, our friends
    are true and our happiness is assured.
    Miles Bader, May 20, 2011
    #4
  5. Miles Bader

    Balog Pal Guest

    "Miles Bader" <> az alábbiakat írta a következõ hírüzenetben:
    ...
    > "Balog Pal" <> writes:
    >>> 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.

    >>
    >> Hm? Call a function that returns what you want, calculated any complex
    >> way you like... You can make it a private static in the class. You can
    >> even make it non-static, but then be extremely careful to use only
    >> members that are already constucted, and spam comments in the class to
    >> avoid reordering.

    >
    > The problem with that is that multiple data members are initialized from
    > the _same_ set of temporaries (maybe I should have stated this more
    > explicitly) -- if it's only a single data member, I do indeed just use a
    > function, and that of course works fine.


    In such situations I use the body of ctor, and just () in the list. It shall
    not be a problem, we have the ctor body for some reason do we? ;-)

    Another best thing is to put all those related members in a separate
    struct/class that will be a meber or base -- if their init is tied likely
    they are bound in some way. Then you can use a single ctor call.

    The rest is too hacky, certainly it is possible to have a smart pointer that
    holds the temp and gets reset when done -- but it is still a footprint both
    in processing and in class size.
    Balog Pal, May 20, 2011
    #5
  6. Miles Bader

    Marc Guest

    Miles Bader wrote:

    > 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".


    A(T1 x,T2 y,T3 z,T4 temp1=f(x,y,z),T5 temp2=g(x,y,z)) :
    x(h(temp1,temp2)),y(i(temp1,temp2)),z(j(temp1,temp2)) {}

    Delegating constructors allow different solutions.
    Marc, May 20, 2011
    #6
  7. Miles Bader

    Balog Pal Guest

    "Marc" <>
    >> 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:
    >>
    >>
    >> but inside constructor initialization forms, one can't create "temp1"
    >> and "temp2".

    >
    > A(T1 x,T2 y,T3 z,T4 temp1=f(x,y,z),T5 temp2=g(x,y,z)) :
    > x(h(temp1,temp2)),y(i(temp1,temp2)),z(j(temp1,temp2)) {}
    >

    int foo(int);
    struct A
    {
    A(int par, int tmp = foo(par)) {}
    };
    void m() { A a(1);}
    - ---
    "ComeauTest.c", line 4: error: a parameter is not allowed
    A(int par, int tmp = foo(par)) {}
    ^

    1 error detected in the compilation of "ComeauTest.c".
    - ----

    Even if worked, you could be bitten if someone passes in a value.

    But the idea is good, and can be pushed to wrking stuff: make the temp param
    a smart pointer, init it early then use. Like (just pseudocode)

    struct T3
    {
    T1 m1;
    T2 m2;
    T3( T1 p1, T2 p2) {...}
    };

    A(T1 p1, T2 p2, auto_ptr<T3> t = 0) :
    m1( t.reset(new T3(p1, p2), t->m1), m2(t->m2) {}

    I still prefer just using the ctor body of A.
    Balog Pal, May 20, 2011
    #7
  8. Miles Bader

    Marc Guest

    "Balog Pal" wrote:

    >> A(T1 x,T2 y,T3 z,T4 temp1=f(x,y,z),T5 temp2=g(x,y,z)) :
    >> x(h(temp1,temp2)),y(i(temp1,temp2)),z(j(temp1,temp2)) {}
    >>

    > int foo(int);
    > struct A
    > {
    > A(int par, int tmp = foo(par)) {}
    > };
    > void m() { A a(1);}
    > - ---
    > "ComeauTest.c", line 4: error: a parameter is not allowed
    > A(int par, int tmp = foo(par)) {}
    > ^


    Indeed, I hadn't tried it, and I am a bit surprised that this is
    forbidden. Well, too bad. But I wonder why they thought it a problem
    to allow using a mandatory argument in the default value of optional
    arguments.

    > Even if worked, you could be bitten if someone passes in a value.


    Bah, just introduce a dummy argument in between:
    struct A {
    struct Never_used_directly{};
    A(int par,Never_used_directly=Never_used_directly(),int tmp=...){}
    };

    (you can make the dummy even harder to accidentally produce, but
    that's the idea)

    > But the idea is good, and can be pushed to wrking stuff: make the temp param
    > a smart pointer, init it early then use. Like (just pseudocode)
    >
    > struct T3
    > {
    > T1 m1;
    > T2 m2;
    > T3( T1 p1, T2 p2) {...}
    > };
    >
    > A(T1 p1, T2 p2, auto_ptr<T3> t = 0) :
    > m1( t.reset(new T3(p1, p2), t->m1), m2(t->m2) {}


    Clever, but I'd rather wait for delegating constructors instead.
    Marc, May 20, 2011
    #8
  9. Miles Bader

    Marc Guest

    Marc wrote:

    > "Balog Pal" wrote:
    >
    >>> A(T1 x,T2 y,T3 z,T4 temp1=f(x,y,z),T5 temp2=g(x,y,z)) :
    >>> x(h(temp1,temp2)),y(i(temp1,temp2)),z(j(temp1,temp2)) {}
    >>>

    >> int foo(int);
    >> struct A
    >> {
    >> A(int par, int tmp = foo(par)) {}
    >> };
    >> void m() { A a(1);}
    >> - ---
    >> "ComeauTest.c", line 4: error: a parameter is not allowed
    >> A(int par, int tmp = foo(par)) {}
    >> ^

    >
    > Indeed, I hadn't tried it, and I am a bit surprised that this is
    > forbidden. Well, too bad. But I wonder why they thought it a problem
    > to allow using a mandatory argument in the default value of optional
    > arguments.


    Apparently it is my mental representation of default arguments that
    didn't quite fit. I always pictured it as forwarding:
    int f(int,int=g());
    was to me something like:
    int f(int,int);
    int f(int x){return f(x,g());}

    whereas the standard presents it without any intermediate layer:
    f(1); is "rewritten" to f(1,g()); but with resolution of g performed
    as if in the forwarding model...
    (and thus the unspecified order of evaluation of function arguments
    prevents from using one parameter in another one)

    It is not super convincing, but at least I see where it comes from
    now...
    Marc, May 20, 2011
    #9
  10. Miles Bader

    Stefan Ram Guest

    Miles Bader <> writes:
    >int temp2 = some_other_long_calculation (arg);


    #include <iostream>

    class alpha
    { int beta; int gamma;

    alpha( int const x, int const y, int const tmp ):
    beta( x / tmp ), gamma( y * tmp )
    { ::std::cout << beta << gamma << "\n"; }

    public: static alpha factory( int const x, int const y )
    { return alpha( x, y, x + y ); }};

    int main(){ ::alpha::factory( 2, 3 ); }
    Stefan Ram, May 20, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,258
    Martijn Lievaart
    Feb 6, 2004
  2. Replies:
    2
    Views:
    561
    Mark P
    May 9, 2005
  3. Zachary  Turner
    Replies:
    5
    Views:
    383
    Zachary Turner
    Jul 13, 2007
  4. A. Farber
    Replies:
    3
    Views:
    244
    Ben Morrow
    Mar 3, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    246
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page