问题描述
在 Java 中,您可以定义泛型类,该类只接受扩展您选择的类的类型,例如:
这是使用extends"关键字完成的.
C++ 中是否有一些简单的等价于 this 关键字?
我建议使用 Boost 的 静态断言 功能与 is_base_of
来自 Boost Type Traits 库:
在其他一些更简单的情况下,您可以简单地向前声明一个全局模板,但只为有效类型定义(显式或部分专门化)它:
[次要编辑 6/12/2013:使用声明但未定义的模板将导致链接器,而不是编译器错误消息.]
In Java you can define generic class that accept only types that extends class of your choice, eg:
This is done using "extends" keyword.
Is there some simple equivalent to this keyword in C++?
I suggest using Boost's static assert feature in concert with is_base_of
from the Boost Type Traits library:
In some other, simpler cases, you can simply forward-declare a global template, but only define (explicitly or partially specialise) it for the valid types:
[Minor EDIT 6/12/2013: Using a declared-but-not-defined template will result in linker, not compiler, error messages.]
这篇关于仅接受某些类型的 C++ 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!