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

C++TemplatesInstantiation

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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 - C++

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 - C++

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.

Solution 4 - C++

If I understand your question correctly, you have a template class, and you want to force the compiler to generate the code for use with some specific type. For example, you may want to ensure the code for std::vector<int> exists in your program.

The best way to ensure this is to simply construct an instance of the class:

void EnsureInstantiation()
{
    std::vector<int> intvector;
    std::vector<boo> boolvector;
    /// etc.
}

The trick is that you don't even have to call EnsureInstantiation anywhere in your code. Just make sure it's not static or else the compiler may optimize it out.

Solution 5 - C++

abstract class cannot be instantiated.you probably want to do something along the lines of:

Abstract *a = new Implementation(...);

To force template instantiation, call template with template parameters:

std::max<int>(...);
std::pair<int, string>(...);

Solution 6 - C++

I'm going to answer what I think you meant, not what you said.

I am guessing the issue is one of two things. The first is that you have code in a template that's not getting compiled when you compile the template file itself, which can be very annoying. That can be fixed in your compiler settings.

The other is you want to have something special for a particular type, perhaps to debug it. That is called explicit instanciation but does not really instanciate anything just makes sure it's always defined after that point.

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/topic/com.ibm.vacpp6m.doc/language/ref/clrc16explicit_instantiation.htm

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanonView Question on Stackoverflow
Solution 1 - C++Georg FritzscheView Answer on Stackoverflow
Solution 2 - C++Alexander PoluektovView Answer on Stackoverflow
Solution 3 - C++sthView Answer on Stackoverflow
Solution 4 - C++AntonView Answer on Stackoverflow
Solution 5 - C++AnycornView Answer on Stackoverflow
Solution 6 - C++Charles Eli CheeseView Answer on Stackoverflow