Explicit instantiation of templates: any experience?

Discussion in 'C++' started by Steven T. Hatton, Dec 4, 2006.

  1. Has anybody here used explicit instantiation of templates? Has it worked
    well? Are there any issues to be aware of?
    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
     
    Steven T. Hatton, Dec 4, 2006
    #1
    1. Advertising

  2. Steven T. Hatton

    Noah Roberts Guest

    Steven T. Hatton wrote:
    > Has anybody here used explicit instantiation of templates? Has it worked
    > well? Are there any issues to be aware of?


    Sure it's worked. Don't do it in headers...don't know what else to
    tell yah.
     
    Noah Roberts, Dec 4, 2006
    #2
    1. Advertising

  3. Steven T. Hatton wrote:
    > Has anybody here used explicit instantiation of templates? Has it
    > worked well? Are there any issues to be aware of?


    The only "problem" or "issue" that I know of is that you have to
    predict what template arguments the users of your templates are
    going to use. What I've often seen are instantiations of some
    calculation templates based on 'float', 'double', 'long double',
    and, if applicable, 'complex' variations of those, which makes
    it possible to hide the implementation in a library (binary) and
    still have only one codebase [for you to maintain].

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 4, 2006
    #3
  4. Steven T. Hatton

    Craig Scott Guest

    > Has anybody here used explicit instantiation of templates? Has it worked
    > well? Are there any issues to be aware of?


    There are issues, but they can be managed. Explicit instantiation is
    perhaps not one of the more common-knowledge parts of templates in C++,
    so consider whether others who may be working on your code will follow
    what you are doing. But this should be a minor issue, since the syntax
    at least is straightforward. To develop a good understanding of this
    topic, I strongly recommend you get hold of "C++ Templates" by
    Vandevoorde and Josuttis, then read section 10.5 on Explicit
    Instantiation and also Appendix A on the One Definition Rule.

    Explicit instantiation requires you to have a very good understanding
    of how your templates will be used. For example, if you explicitly
    instantiate a non-specialized class, you are enforcing that the class
    cannot be specialised in any other code (that would be an ill-formed
    C++ program). You have to make it very clear to clients of your class
    that they cannot create an explicit specialization for the particular
    type you explicitly instantiated. This can get ugly pretty fast if
    there are more than a couple of types involved.

    A case where explicit instantiation is particularly useful, however, is
    when you have a base class with virtual functions and you make a
    template subclass for it. Interestingly, if you instantiate an object
    of the subclass, the C++ standard does not require the compiler to
    instantiate all the virtual functions from the subclass. Therefore, if
    you pass the object around by its base pointer, any client code calling
    the virtual functions through that base pointer will not trigger an
    implicit instantiation of the subclass' virtual function. In practice,
    I believe compilers will generally instantiate all virtual functions
    when an object is instantiated, since not to do so is generally
    counter-intuitive for the average programmer, but I am not willing to
    make a blanket statement that they all do. Perhaps someone else can
    impart some wisdom here? The relevant part of the C++ standard is
    14.7.1.9:

    "... It is unspecified whether or not an implementation implicitly
    instantiates a virtual member function of
    a class template if the virtual member function would not otherwise be
    instantiated."

    --
    Computational Fluid Dynamics, CSIRO (CMIS)
    Melbourne, Australia
     
    Craig Scott, Dec 5, 2006
    #4
    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. Rolf Magnus
    Replies:
    0
    Views:
    881
    Rolf Magnus
    Jul 23, 2003
  2. C. Carbonera
    Replies:
    4
    Views:
    1,384
    C. Carbonera
    Feb 5, 2004
  3. Fernando Cuenca
    Replies:
    4
    Views:
    2,557
    Gianni Mariani
    Sep 6, 2004
  4. Replies:
    5
    Views:
    406
    Victor Bazarov
    Jul 14, 2005
  5. Replies:
    1
    Views:
    616
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page