如何强制 C++ 模板的特定实例进行实例化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2152002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force a particular instance of a C++ template to instantiate?
提问by anon
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:
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 以及英特尔编译器构建库。
回答by Georg Fritzsche
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 FAQcovers the related issues in some detail.
Comeau模板 FAQ 详细介绍了相关问题。
回答by Alexander Poluektov
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
回答by sth
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.
您不需要在任何地方实际调用该函数,因此指针未初始化不是问题。但是编译器必须假设该函数可能是从另一个目标文件调用的,因此它必须实例化模板。
回答by Anton
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<int> 的代码。
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 mayoptimize it out.
诀窍是您甚至不必在代码中的任何地方调用EnsureInstantiation。只要确保它不是静态的,否则编译器可能会优化它。
回答by Anycorn
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>(...);
回答by Charles Eli Cheese
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.
另一个是您想要为特定类型提供一些特殊的东西,也许是为了调试它。这称为显式实例化,但并没有真正实例化任何东西,只是确保它始终在该点之后定义。