[英]How can I enforce that a template parameter used implements some interface in C++?
我不認為這在C ++中是可能的,我有什么選擇來模擬行為?
使用std::is_base_of
為:
template<typename T>
class A
{
static_assert(std::is_base_of<IMyInterface, T>::value,
"T must derive from IMyInterface");
};
您也可以在功能模板中使用相同的功能。
您可以使用std::is_base_of<YourInterface, YourParameter>
,如果結果為false
,則會出錯。 記住這是C ++ 11。
一些多態接口還是一些靜態接口? 后者可以通過Boost Concepts檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.