Thread: Templates

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking Templates

    I am implementing a sort of array in my class. Its somewhat like this.
    Code:
    //blah...blah...
    template<int Size = 10>
    class Array{
    private:
        double  a[Size];
    //blah...blah..
    };
    Now I understand that for each different type of instantaniation of an object using a template, the compiler does a sort of 'copying and pasting the code' making necessary changes and avoiding pitfalls. So if I make, say like 5 small arrays at different places,
    Code:
    int main(){
    //blah...blah...
    Array<4> one;
    Array<3> two;
    Array<10> three;
    Array<15> four;
    Array<8> five;
    //blah...blah...
    }
    Will my program code become very large when the compiler 'copies and pastes' 5 different types of array classes.
    Is dynamic allocaton of arrays better in this case?

    P.S I am using templates to avoid 'cleaning up after myself' after doing dynamic allocations and also to learn templates in detail.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    P.S I am using templates to avoid 'cleaning up after myself' after doing dynamic allocations
    How does that work?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The answer to your question is really "it depends". The size of code generated in response to multiple instantiations of a template is an issue of quality of implementation of the compiler. Some compilers give code bloat, and others don't. As a rough rule, the older the compiler, the more likely it is to give code bloat. In your example above, the bloat associated with 5 instances of your template is likely to be comparable to what you would get if you had to roll-your-own 5 distinct classes for different arrays sizes.

    There is an issue, with some compilers, of instantiating templates locally to a compilation -- meaning that, if a particular template instance is used in two distinct source files, both object files will contain that instance, and both instances wind up in the exectutable. Again, more modern compilers (and the development environments they are part of) are less likely to have this issue.

    Some compilers (eg gcc) also have options related to explicit instantiation of templates, which can be used to reduce the code bloat associated with templates being instantiated in multiple files that are passed to the linker.

    Out of curiosity, why not use a std::vector<double>? One characteristic of a std::vector is that you can set it's size.

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Quote Originally Posted by 7stud
    How does that work?
    No when using templates I don't dynamically allocate memmory.
    But if templates make my code big, then I'll have to change my implementation to dynamic allocation rather than templates.
    I'll check my compiler (gcc) for the troubles you pointed out...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Check out boost::array (http://www.boost.org/doc/html/array.html). It has been added to TR1 and will be part of the next standard. It may have the same issues with code size, but it has already been implemented so if you wanted to go with a statically sized array that would be the better choice then rolling your own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM