templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chameleon

    #1

    templates

    When I use

    vector<int>
    vector<int*>
    vector<anything *>

    I have 3x code, or because of same size:
    sizeof int == sizeof any pointer
    I have only 1x code?



    and another question:

    I use vector<int> somewhere.
    If I use (in another position) list<int>, it increases the binary more
    that I use again vector<int>?
  • Victor Bazarov

    #2
    Re: templates

    Chameleon wrote:[color=blue]
    > When I use
    >
    > vector<int>
    > vector<int*>
    > vector<anything *>
    >
    > I have 3x code, or because of same size:
    > sizeof int == sizeof any pointer
    > I have only 1x code?[/color]

    You have exactly 1x code which you actually use. If you use
    'vector<int>::p ush_back', it will be generated. If you don't
    use 'vector<anythin g*>::clear', it will not be generated.
    [color=blue]
    > and another question:
    >
    > I use vector<int> somewhere.
    > If I use (in another position) list<int>, it increases the binary more
    > that I use again vector<int>?[/color]

    Yes, most likely.

    V
    --
    Please remove capital As from my address when replying by mail


    Comment

    • Phlip

      #3
      Re: templates

      Chameleon wrote:
      [color=blue]
      > When I use
      >
      > vector<int>
      > vector<int*>
      > vector<anything *>
      >
      > I have 3x code, or because of same size:
      > sizeof int == sizeof any pointer
      > I have only 1x code?[/color]

      Some compilers advertise their template implementations can fold identical
      code, as a hedge against binary bloat.

      Some template libraries use secret internal typecasts to bust all pointers
      down to integers, process them, and cast them back.

      You should never do that in your own code, never rely on it, and you should
      only rarely worry about code footprint.
      [color=blue]
      > and another question:
      >
      > I use vector<int> somewhere.
      > If I use (in another position) list<int>, it increases the binary more
      > that I use again vector<int>?[/color]

      Yes. list and vector are completely different, and will always express
      different opcodes into your binary. (And always prefer vector without a
      reason to use something else.)

      However, all these guestimates hit the C++ "As If" rule. It states the
      compiler can do anything it likes so long as the resulting code performs As
      If it had obeyed the ISO C++ Standard's compilation model. (Put another way,
      this permits the ISO committees to define C++ in terms of hardware, as a
      convenience, without the definition growing wildly abstract.)

      However however, the most important resource to optimize is programmer time.
      Don't spend too much time worrying about the speed or size of each line of
      code you write. Do worry about code clarity. It's easier to make beautiful
      code fast and small than to make fast or small code beautiful.

      --
      Phlip
      http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


      Comment

      Working...