类模板可以实例化没有成员? [英] Class template can be instantiated without members?

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

问题描述

维基百科文章说明了这一点:


实例化类模板不会导致其成员定义被实例化。

instantiating a class template does not cause its member definitions to be instantiated.

我无法想象任何类

推荐答案

许多早期的C ++编译器被实例化

Many early C++ compilers instantiated all member functions, whether you ever called them or not.

例如,考虑 std :: list ,它有一个 sort 成员函数。使用当前正常运行的编译器,您可以在不支持比较的类型上实例化 list 。如果您尝试使用 list :: sort ,它将失败,因为您不支持比较。只要你不为该列表调用 sort ,就可以了,因为 list< T> :: sort

Consider, for example, std::list, which has a sort member function. With a current, properly functioning compiler, you can instantiate list over a type that doesn't support comparison. If you try to use list::sort, it will fail, because you don't support comparison. As long as you don't call sort for that list, it's all fine though, because list<T>::sort won't be instantiated unless you call it.

然而,对于那些老的,运行不良的编译器,试图创建 list< T> 意味着 list< T> :: sort 已实例化,即使您从未使用它。存在 list :: sort 意味着您需要为 T / code>,即使您从未实际使用过 sort ,也只是为了创建列表< T&该类型的列表。

With those older, poorly functioning compilers, however, trying to create list<T> meant that list<T>::sort was instantiated even though you never used it. The existence of list::sort meant that you needed to implement < for T, just to create a list<T>, even if you never actually used sort on a list of that type at all.

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

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