检查类模板是否已实例化? [英] Checking whether a class template has been instantiated?

查看:62
本文介绍了检查类模板是否已实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简便的方法来查看某个类是否已在翻译单元中实例化?C ++ Primer的一项练习要求每个标记的语句是否实例化:

Is there an easy way to see whether a class has been instantiated in a translation unit? An exercise from C++ Primer asks for each labelled statement, whether an instantiation happens:

template <typename T> class Stack { };
void f1(Stack<char>); // (a)
class Exercise {
    Stack<double> &rsd; // (b)
    Stack<int> si; // (c)
};
int main() {
    Stack<char> *sc; // (d)
    f1(*sc); // (e)
    int iObj = sizeof(Stack< string >); // (f)
}

我不确定如何才能真正检查我的答案.我以为也许可以为每种类类型使用显式实例化(例如 extern模板类Stack< char> ),然后在程序中再也没有相应的显式实例化定义.这样,如果实例化了某些东西,如果以后没有出现定义,则链接器将引发错误.

I'm not sure how I could actually check my answers for these. I thought maybe I could use explicit instantiations for each class type (e.g. extern template class Stack<char>) and then never have a corresponding explicit instantiation definition in the program. That way if something was instantiated, if the definition didn't later appear then the linker would kick up an error.

但是,编译器/链接器并不总是能够识别出这样的错误:

However the compiler/linker doesn't always recognise such an error:

template <typename T> class A{ };
extern template class A<int>;
int main(){
    A<int> a; 
}

这在gcc 4.9.2上可以正常编译.但是,从我从N3337的[14.7.2] [11]可以看出的情况来看,如果这是程序中唯一的目标文件,应该是一个错误:

This compiles fine on gcc 4.9.2. However if this was the only object file in my program is should be an error as far as I can tell from [14.7.2][11] of N3337:

如果实体是同一翻译单元中的显式实例化声明和显式实例化定义的主题,则该定义应位于声明之后.作为以下主题的实体显式实例化声明,并且还应以其他方式在翻译单元中引起隐式实例化(14.7.1)的方式使用,该隐式实例化声明应是程序中某个处的显式实例化定义的主题;否则程序格式错误,无需诊断.

If an entity is the subject of both an explicit instantiation declaration and an explicit instantiation definition in the same translation unit, the definition shall follow the declaration. An entity that is the subject of an explicit instantiation declaration and that is also used in a way that would otherwise cause an implicit instantiation (14.7.1) in the translation unit shall be the subject of an explicit instantiation definition somewhere in the program; otherwise the program is ill-formed, no diagnostic required.

(我想不需要诊断"就是为什么它不会引发错误?).另外,是否有实例化发生在不完整的类类型不适用于表达式的情况下-以便我可以通过删除 Stack 的定义来进行检查?

(I'm guessing the "no diagnostic required" is why this doesn't kick up an error?). Alternatively is it the case that instantiations happen whenever an incomplete class type isn't viable for an expression - so that I could check by removing the definition of Stack?

template <typename T> class Stack;

每个不完整的类型错误对应一个实例化发生的地方吗?

So that each incomplete type error corresponds to a place where an instantiation would have occured?

推荐答案

您可以在可执行文件上使用nm工具.这将显示哪个文件包含函数定义.此外,gcc还提供了一个标记,可在进行链接时清除未使用的功能.

You can use the nm tool on the executable. This will show what file contains function definitions. Also gcc provides a flag to strip out unused functions when doing the link.

使用"-fdata-sections"进行编译以将数据保留在单独的数据节中,使用"-ffunction-sections"进行编译以将函数保留在单独的节中,因此如果不使用它们(数据和函数),则可以将其丢弃.链接到"–gc-sections"以删除未使用的部分.

Compile with "-fdata-sections" to keep the data in separate data sections and "-ffunction-sections" to keep functions in separate sections, so they (data and functions) can be discarded if unused. Link with "–gc-sections" to remove unused sections.

这篇关于检查类模板是否已实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆