Templates for syntatic niceness only, does it waste space?

Discussion in 'C++' started by collection60@hotmail.com, Jul 23, 2006.

  1. Guest

    Hi people,

    I want to use templates, for syntatic niceness only, not for doing
    similar operations on different types. The types I'm manipulating are
    basically just pointers. So if it's a pointer to a Fred or a pointer to
    a long, it makes no difference.

    So, I want the exact same code, to be running on different types. So
    even if I used this template on 1000 different types, I'd want only one
    copy of that code in my executable, I do not want the size of my
    executable to go up with the number of types used by this template.

    Is that how it works? Can I get syntactic niceness without getting code
    bloat?
     
    , Jul 23, 2006
    #1
    1. Advertising

  2. Uday Bidkar Guest

    Hi

    In case of templates, a separate code is generated for each type being
    used in your code.
    In fact, no code is generated if you dont use the template in your code
    at all but just declare it. i.e., the code
    template<typename T>

    void Foo( T t )
    {
    These are garbage lines,
    aaaa
    bbbb
    cccc
    /+*-
    }
    int main()
    {
    return 0 ;
    }

    will compile successfuly as you are calling Foo nowhere

    So, the size of object files generated depends upon the number of
    types you use the template for and hence the size of the executable.

    Regards,
    Uday Bidkar

    wrote:
    > Hi people,
    >
    > I want to use templates, for syntatic niceness only, not for doing
    > similar operations on different types. The types I'm manipulating are
    > basically just pointers. So if it's a pointer to a Fred or a pointer to
    > a long, it makes no difference.
    >
    > So, I want the exact same code, to be running on different types. So
    > even if I used this template on 1000 different types, I'd want only one
    > copy of that code in my executable, I do not want the size of my
    > executable to go up with the number of types used by this template.
    >
    > Is that how it works? Can I get syntactic niceness without getting code
    > bloat?
     
    Uday Bidkar, Jul 23, 2006
    #2
    1. Advertising

  3. mlimber Guest

    wrote:
    > Hi people,
    >
    > I want to use templates, for syntatic niceness only, not for doing
    > similar operations on different types. The types I'm manipulating are
    > basically just pointers. So if it's a pointer to a Fred or a pointer to
    > a long, it makes no difference.
    >
    > So, I want the exact same code, to be running on different types. So
    > even if I used this template on 1000 different types, I'd want only one
    > copy of that code in my executable, I do not want the size of my
    > executable to go up with the number of types used by this template.
    >
    > Is that how it works? Can I get syntactic niceness without getting code
    > bloat?


    If you're using the same code to manipulate many disparate pointer
    types, chances are you're not doing much significant with them (e.g.,
    you aren't using the interface for Fred, using them polymorphically,
    etc.). So just declare the function inline, and things will likely be
    the same (since "inline" is only a hint) as if you wrote that pointer
    manipulating code in each place where you call the function.

    Cheers! --M
     
    mlimber, Jul 23, 2006
    #3
  4. wrote:

    > I want to use templates, for syntatic niceness only, not for doing
    > similar operations on different types. The types I'm manipulating are
    > basically just pointers. So if it's a pointer to a Fred or a pointer to
    > a long, it makes no difference.
    >
    > So, I want the exact same code, to be running on different types. So
    > even if I used this template on 1000 different types, I'd want only one
    > copy of that code in my executable, I do not want the size of my
    > executable to go up with the number of types used by this template.


    You can specialize your template for void *, an make a partially
    specialization for pointer types that uses it. "The C++ programming
    language" shows how to do that.

    Or write a non-templated class or function for void *, and use it, or
    inherit from it, in the template.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jul 23, 2006
    #4
  5. Guest

    Well, think of the hash_map class.

    Let's say I have a hash_map relating strings to long*, another for
    strings to Fred* and another for strings to X*.

    Well, the code is entirely interchangable. I'd hope that the compiler
    knows this. Know it seems that it doesn't?

    > If you're using the same code to manipulate many disparate pointer
    > types, chances are you're not doing much significant with them (e.g.,
    > you aren't using the interface for Fred, using them polymorphically,
    > etc.). So just declare the function inline, and things will likely be
    > the same (since "inline" is only a hint) as if you wrote that pointer
    > manipulating code in each place where you call the function.
    >
    > Cheers! --M
     
    , Jul 23, 2006
    #5
  6. Bo Persson Guest

    <> skrev i meddelandet
    news:...
    > Hi people,
    >
    > I want to use templates, for syntatic niceness only, not for doing
    > similar operations on different types. The types I'm manipulating
    > are
    > basically just pointers. So if it's a pointer to a Fred or a pointer
    > to
    > a long, it makes no difference.
    >
    > So, I want the exact same code, to be running on different types. So
    > even if I used this template on 1000 different types, I'd want only
    > one
    > copy of that code in my executable, I do not want the size of my
    > executable to go up with the number of types used by this template.
    >
    > Is that how it works? Can I get syntactic niceness without getting
    > code
    > bloat?
    >


    Some compilers will recognize that the code is identical for several
    types, and merge the code. Some will not.

    This is a typical quality of implementation thing, the language
    standard doesn't say what will happen.


    Bo Persson
     
    Bo Persson, Jul 23, 2006
    #6
  7. Old Wolf Guest

    Uday Bidkar wrote:
    > Hi
    >
    > In case of templates, a separate code is generated for each type being
    > used in your code.


    Not necessarily; most compilers will generate the same code
    for Foo<long> as for Foo<int> (if they have int and long both as
    the same size).

    > In fact, no code is generated if you dont use the template in your code
    > at all but just declare it. i.e., the code
    > template<typename T>
    >
    > void Foo( T t )
    > {
    > These are garbage lines,
    > aaaa
    > bbbb
    > cccc
    > /+*-
    > }
    > int main()
    > {
    > return 0 ;
    > }
    >
    > will compile successfuly as you are calling Foo nowhere


    Not true. The code for Foo() must be syntactically correct.
    (Your particular compiler might compile it, but others won't).

    Imagine what would happen if the garbage lines contained
    a "}" character. How does the compiler know where the end
    of the function is? You can' t just search for braces; this is
    a valid function:

    void foo()
    {
    X(})
    }

    Hopefully you can now see some of the reasons why the code
    has to follow correct C++ syntax.
     
    Old Wolf, Jul 24, 2006
    #7
  8. mlimber Guest

    wrote:
    > > If you're using the same code to manipulate many disparate pointer
    > > types, chances are you're not doing much significant with them (e.g.,
    > > you aren't using the interface for Fred, using them polymorphically,
    > > etc.). So just declare the function inline, and things will likely be
    > > the same (since "inline" is only a hint) as if you wrote that pointer
    > > manipulating code in each place where you call the function.

    >
    > Well, think of the hash_map class.
    >
    > Let's say I have a hash_map relating strings to long*, another for
    > strings to Fred* and another for strings to X*.
    >
    > Well, the code is entirely interchangable. I'd hope that the compiler
    > knows this. Know it seems that it doesn't?


    (It's the custom here to put your reply inline or after the post you
    are responding to. I fixed yours here.)

    As Bo Persson said, it is more of a QoI issue as to whether the
    compiler will actively combine code for classes and functions generated
    from templates, but even with the STL, my point stands: most STL
    containers' member functions (and those of soon-to-be STL containers
    like hash_map, aka std::tr1::unordered_map) are inline by default,
    which will often amount to the same thing as combining code.

    Cheers! --M
     
    mlimber, Jul 24, 2006
    #8
  9. Guest

    wrote:
    > Hi people,
    >
    > I want to use templates, for syntatic niceness only, not for doing
    > similar operations on different types. The types I'm manipulating are
    > basically just pointers. So if it's a pointer to a Fred or a pointer to
    > a long, it makes no difference.
    >
    > So, I want the exact same code, to be running on different types. So
    > even if I used this template on 1000 different types, I'd want only one
    > copy of that code in my executable, I do not want the size of my
    > executable to go up with the number of types used by this template.


    Typically, an optimizer looks at the bytes generated. If two functions
    have
    the same bytes, they usually can have the same address. Of course, this
    requires the optimizer to be run - "debug" builds will usually keep
    them
    distinct so you can understand what's going on.

    But what's stopping you from trying? If the compiler fails, you can
    always add
    the non-nice form yourself and make the syntactically nice versions
    into
    one-line inlined wrappers. Same result, but with more work for you.

    HTH,
    Michiel Salters
     
    , Jul 24, 2006
    #9
    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. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,936
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,717
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,053
    Steve Lamb
    Oct 23, 2003
  4. =?koi8-r?b?+sHV0iA=?= =?koi8-r?b?+8nC2tXIz9c=?=

    Set/Get attribute syntatic sugar

    =?koi8-r?b?+sHV0iA=?= =?koi8-r?b?+8nC2tXIz9c=?=, Jun 28, 2005, in forum: Python
    Replies:
    9
    Views:
    363
  5. Roedy Green

    syntatic sugar IN keyword.

    Roedy Green, Mar 17, 2011, in forum: Java
    Replies:
    13
    Views:
    421
    Jim Janney
    Mar 18, 2011
Loading...

Share This Page