How to force an instance of a C ++ instance for an instance?

See chapter. I have a template. I want to force a specific template instance to instantiate. How to do it?

In particular, can you force an abstract template class to be created?




I could clarify, since I have the same question. In my case, I create a library, some of the template implementations are large and contain a lot of materials, but are created for only a few types. I want to compile them in the library and export all the methods, but not include the header with the code everywhere.

t

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, refer to all the methods inside the library that would force them to compile and export, but the desire not to add unnecessary code to the library, like formatting arguments for elements and code for calling them, etc.

????? In particular, I am creating a library for several versions of MSC and GCC and Intel compilers.

+40
c ++ instantiation templates
Jan 28 '10 at 3:01
source share
6 answers

You cannot force templates to instantiate; the compiler can only generate code if the type is fully known.

Forced instantiation is done by explicitly providing all types:

 template class std::vector<int>; 

The parish frequently asked questions about the watch describes some of the problems.

+45
Jan 28 '10 at 3:11
source share
β€” -

What you can also try is to explicitly instantiate:

 template class vector<int>; // class template int& vector<int>::operator[](int); // member template int convert<int,double>(double); // function 
+38
Jan 28 '10 at 15:50
source share

You can force an instance using the template with the desired parameter. For example, you can define a function using all the necessary methods:

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

You do not need to call this function anywhere, so the problem is not that the pointer is not initialized. But the compiler should assume that the function can be called from another object file, so it must create an instance of the template.

+1
Jan 28 '10 at 3:31
source share

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

The best way to provide this is to simply instantiate the class:

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

The trick is that you don't even need to call EnsureInstantiation anywhere in your code. Just make sure that it is not static or that the compiler can optimize it.

0
Jan 28 '10 at 3:27
source share

An abstract class cannot be created. You probably want to do something according to:

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

To force the creation of a template, call the template with the template parameters:

 std::max<int>(...); std::pair<int, string>(...); 
-one
Jan 28 '10 at 3:10
source share

I am going to answer what I think you had in mind, not what you said.

I guess the problem is one of two things. Firstly, you have code in a template that does not compile when compiling the template file itself, which can be very annoying. This can be fixed in the settings of your compiler.

Another - you want to have something special for a certain type, perhaps for debugging it. This is called explicit instanciation, but does not actually stimulate anything, it simply ensures that it will always be determined after this point.

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

-2
Jan 28 '10 at 7:09
source share



All Articles