Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 828
anon Avatar asked Jan 28 '10 03:01

anon


People also ask

How do you instantiate an object in a template class?

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments. template class Array<char>; template class String<19>; When you explicitly instantiate a class, all of its members are also instantiated.

How many times is the template class instantiation?

cpp, there are two instantiations: template class B<int> and B<float>.

What is instantiation of a template?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation to handle a specific set of template arguments is called a specialization.

Is it necessary to instantiate a template?

In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).


2 Answers

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.

like image 70
Georg Fritzsche Avatar answered Sep 21 '22 03:09

Georg Fritzsche


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
like image 44
Alexander Poluektov Avatar answered Sep 19 '22 03:09

Alexander Poluektov