How do I force a particular instance of a C++ template to instantiate?

34,147

Solution 1

You can't force generic templates to instantiate, the compiler can only generate code if the type is completely known.

Forcing an instantiation is done by providing all types explicitly:

template class std::vector<int>;

Comeaus template FAQ covers the related issues in some detail.

Solution 2

What you also can try is explicit instantiation:

template class vector<int>;                    // class
template int& vector<int>::operator[](int);    // member
template int convert<int,double>(double);      // function

Solution 3

You can force instantiation by using the template with the desired parameter. For example you could define a function using all the required methods:

void force_int_instance() {
  Abstract<int> *a;
  a->some_method();
  a->some_other_method(1, 2, 3);
}

You don't need to actually call that function anywhere, so it's not a problem that the pointer is not initialized. But the compiler has to assume that the function might be called from another object file, so it has to instantiate the template.

Share:
34,147
anon
Author by

anon

Updated on July 09, 2022

Comments

  • anon
    anon almost 2 years

    See title. I have a template. I want to force a particular instance of a template to instantiate. How do I do this?

    More specifically, can you force an abstract template class to instantiate?


    I might elaborate as I have the same question. In my case I am building a library, some of the template implementations are large and include lots of stuff, but are only generated for a couple of types. I want to compile them in the library and export all the methods, but not include the header with the code everywhere.

    ie:

    template<class T>
    OS_EXPORT_DECL class MyTmpl
    {
        T *item1;
    public:
        inline T *simpleGetT() { return(item1); } /* small inline code in here */ } 
        T *doSomeReallyBigMergeStuff(T *b); // note only declaration here
    };
    
    // *** implementation source file only seen inside library
    
    template<class T>
    MyTmpl<T>::doSomeReallyBigMergeStuff(T *b)
    {
        ... a really big method, but don't want to duplicate it, 
            so it is a template ...
    }
    

    I could of course reference all the methods inside the library which would force them to compile and export but the desire isn't to add un-needed code to the library like the argument formatting for the items and the code to call them etc.

    ????? specifically I am building the library for several versions of MSC and GCC and intel compilers.

  • peterk
    peterk over 12 years
    note you should do this after all templates and code for them have been declared to be safe.
  • twerdster
    twerdster almost 12 years
    Until I reached this post I tried for days to use template<> and I couldnt figure out what the issue was. Using just "template" with no angle brackets worked perfectly.
  • sehe
    sehe over 11 years
    @peterk "declared to be safe" had me wondering for about 20 seconds. Then it dawned on me "after ... been declared, to be safe". -- lol
  • Redmumba
    Redmumba almost 11 years
    This is actually a syntax error, at least on gcc 4.1; it should be template class std::vector<int> (Alexander's answer below is correct)