Reply to comment

Some truth... but both compilation time and size may be smaller

This comment is true in that the code is compiled once for each instantiation thus increasing compile time. On the other hand Jason is right pointing out that the linker is responsible for eliminating redundant code. If you list the symbols in all compilation units you will see that all template generated code is marked as a weak symbol implying that it will be removed if an identical normal defined (or weak) symbol, without errors (this makes it One-Definition-Rule so important, when the linker eliminates symbols it depends on the symbols being exactly the same).

Going back to compilation time, C++ standard explicitly states that only instantiated member functions are compiled, so it might be the case that the code that is compiled is smaller in the templated version (if some of the methods of the class are never called), which can reduce both compilation time and final binary size. If you only use std::vector::push_back, std::vector::begin and std::vector::end you will never compile std::vector::operator[], std::vector::insert...

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.