[英]How can I enforce that a template parameter used implements some interface in C++?
我不认为这在C ++中是可能的,我有什么选择来模拟行为?
Use std::is_base_of
as: 使用
std::is_base_of
为:
template<typename T>
class A
{
static_assert(std::is_base_of<IMyInterface, T>::value,
"T must derive from IMyInterface");
};
You can same in function template as well. 您也可以在功能模板中使用相同的功能。
You can use std::is_base_of<YourInterface, YourParameter>
, and make an error if the result is false
. 您可以使用
std::is_base_of<YourInterface, YourParameter>
,如果结果为false
,则会出错。 Remember this is C++11. 记住这是C ++ 11。
Some polymorphic interface or some static interface? 一些多态接口还是一些静态接口? The latter can be checked by Boost Concepts .
后者可以通过Boost Concepts检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.