• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Development Tools » TI C/C++ Compiler » TI C/C++ Compiler - Forum » Late template instantiation
Share
TI C/C++ Compiler
  • Forum
Options
  • Subscribe via RSS

Late template instantiation

Late template instantiation

  • mlimber
    Posted by mlimber
    on Jan 03 2013 10:33 AM
    Prodigy140 points

    I read the TI wiki article on C++ template instantiation issues, and though my situation is different than the main problem depicted, I'm wondering if I could use one of the tricks therein it to speed up my compile times.

    My code is something like this:



    // File Base.hpp

    template<class T>
    struct Base
    {
         virtual ~Base() {}
         virtual void Fn( const T& ) = 0;
    };



    // File Derived1.hpp

    #include "Base.hpp"
    struct Derived1 : Base<int>
    {
         virtual void Fn( const T& );
    };



    // File Derived1.cpp

    #include "Derived1.hpp"
    void Derived1::Fn( const T& t )
    {
         // ...

    }



    // File Derived2.hpp

    #include "Base.hpp"
    struct Derived2 : Base<double>
    {
         virtual void Fn( const T& );
    };



    // File Derived2.cpp

    #include "Derived2.hpp"
    void Derived2::Fn( const T& t )
    {
         // ...

    }



    // File: Main.cpp

    #include "Derived1.hpp"
    #include "Derived2.hpp"

    int
    main()
    {
         Derived1 d1;
         Derived2 d2;
         d1.Fn( 42 );
         d2.Fn( 4.2 );
         return 0;
    }

    The difference here is that I'm linking all these cpp files together in one executable, not linking to a separate lib file, but the link times for my program are quite long, which I think may be a result of late template instantiations, which triggers re-compilation of the cpp files at link time. Would you expect adding explicit instantiations along the lines of: 

    template class Base<int>;
    template class Base<double>;

    to be of any help here? I tried adding each in Derived1.cpp and Derived2.cpp respectively, but then the linker gives duplicate symbol errors on the vtable. I can add them to Main.cpp and it links 3 minutes faster than without. Why can't I put the statements in the more logical location of the Derived?.cpp files? (In my real code, I have three derived classes, and only one of them produces a linker error when the explicit instantiations are put in the derived classes .cpp files.)

    Is it possible that this could actually slow the total time for one or more files down depending on, say, the link order? For instance, say the explicit instantiations are in Derived1.cpp and Derived2.cpp, but the order of the files given to the linker is Derived1.obj, Main.obj, then Derived2.obj. Since the linker hasn't seen an instantiation of the Base<double> yet when it gets to Main.obj, does it recompile and instantiate it in Main.obj even though I have forced it to be instantiated at compile-time in Derived2.cpp? Does this mean that Base<int> was compiled/instantiated only once whereas Base<double> is instantiated twice, with one instantiation being thrown away at link time (or perhaps it isn't thrown away, and that is why I get the vtable linker error)?

    I'm using CGTools 5.1.12. What is the best way to speed up building of template-laden code? Is there any way to prevent recompilation and late (possibly redundant? see above) template instantiation?

    c++ linker template instantiation vtable
    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
  • adavis
    Posted by adavis
    on Jan 03 2013 10:13 AM
    Prodigy270 points

    The example is syntactically invalid as written. I had to change "T" to the actual types int and double in the derived classes. Either that or the derived classes themselves need to be templates. I'm not sure what the intention was, so I chose the first approach.

    Notwithstanding, the explicit instantiations should work as you propose. I changed "T", then tried it with 5.1.12 and it compiled and linked OK. I suspect that whatever you cut out of the declarations is triggering vtable generation. Perhaps a more complete example would help us reproduce the issue.

    But even without explicit instantiations, the late instantiation mechanism should result in subsequent compiles being as fast as having the explicit instantiations. The compiler "remembers" that it needs Base<int> and Base<double> and should instantiate them on subsequent compiles.

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
  • adavis
    Posted by adavis
    on Jan 03 2013 10:33 AM
    Prodigy270 points

    I see now that this is an old thread; sorry to have re-raised it. I'll try to close it.

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
TI E2E™ Community
  • Support Forums
  • Blogs
  • Videos
  • Groups
  • Site Support & Feedback
  • Settings
TI E2E™ Community Groups
  • TI University Program
  • Make the Switch
  • Microcontroller Projects
  • Motor Drive & Control
Other Communities
  • Deyisupport
  • Designsomething.org
  • beagleboard.org
  • TI on Element 14
  • TI on TechXchangeSM
Other Technical & Support Resources
  • WEBENCH® Design Center
  • Product Information Centers
  • Technical Documents
  • TI Design Network
  • TI Technical Articles
  • TI Training

All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
embedded processors, along with software, tools and the industry’s largest sales/support staff.

© Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy Policy | Terms of Use