如何强制一个c++模板的实例实例化?

时间:2022-07-14 15:03:43

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.

? ? ? ? ?具体地说,我正在为MSC、GCC和intel编译器的几个版本构建库。

6 个解决方案

#1


46  

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.

comaus模板FAQ详细介绍了相关问题。

#2


41  

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

#3


1  

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.

你不需要在任何地方调用那个函数,所以指针没有初始化不是问题。但是编译器必须假设函数可以从另一个对象文件中调用,因此它必须实例化模板。

#4


0  

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.

如果我正确地理解了您的问题,那么您有一个模板类,并且您希望强制编译器生成用于某些特定类型的代码。例如,您可能希望确保程序中存在std::vector 的代码。

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.

诀窍在于,您甚至不必在代码中的任何地方调用EnsureInstantiation。只要确保它不是静态的,否则编译器可能会优化它。

#5


-1  

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>(...);

#6


-2  

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

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

#1


46  

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.

comaus模板FAQ详细介绍了相关问题。

#2


41  

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

#3


1  

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.

你不需要在任何地方调用那个函数,所以指针没有初始化不是问题。但是编译器必须假设函数可以从另一个对象文件中调用,因此它必须实例化模板。

#4


0  

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.

如果我正确地理解了您的问题,那么您有一个模板类,并且您希望强制编译器生成用于某些特定类型的代码。例如,您可能希望确保程序中存在std::vector 的代码。

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.

诀窍在于,您甚至不必在代码中的任何地方调用EnsureInstantiation。只要确保它不是静态的,否则编译器可能会优化它。

#5


-1  

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>(...);

#6


-2  

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

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