• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C++11模板元编程-std::enable_if示例详解

c++ 搞代码 4年前 (2022-01-06) 20次浏览 已收录 0个评论

这篇文章主要给大家介绍了关于C++11模板元编程-std::enable_if的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

C++11中引入了std::enable_if函数,函数原型如下:

 template struct enable_if; 

可能的函数实现:

 template struct enable_if {}; template struct enable_if { typedef T type; }; 

由上可知,只有当第一个模板参数为true时,enable_if会包含一个type=T的公有成员,否则没有该公有成员。

头文件:

 #include  

std::enable_if使用场景

1、限制模板函数的参数类型

在某些场景下,我们需要实现只有特定类型可以调用的模板函数。如下代码所示,通过对返回值使用std::enable_if和在模板参数中使用std::enable_if均实现了只允许整形参数调用函数的功能。

 // enable_if example: two ways of using enable_if #include  #include  // 1. the return type (bool) is only valid if T is an integral type: template  typename std::enable_if<std::is_integral::value,bool>::type is_odd (T i) {return bool(i%2);} // 2. the second template argument is only valid if T is an integral type: template <class t, class=typename <span style="color:transparent">来源gaodai#ma#com搞*!代#%^码网</span>std::enable_if<std::is_integral::value>::type> bool is_even (T i) {return !bool(i%2);} int main() { short int i = 1;  // code does not compile if type of i is not integral std::cout << std::boolalpha; std::cout << "i is odd: " << is_odd(i) << std::endl; std::cout << "i is even: " << is_even(i) << std::endl; return 0; }

当使用float类型参数调用函数时,程序会报错:

error: no matching function for call to ‘is_odd(float&)’

2. 模板类型偏特化

在使用模板编程时,可以利用std::enable_if的特性根据模板参数的不同特性进行不同的类型选择。

如下所示,我们可以实现一个检测变量是否为智能指针的实现:

 #include  #include  #include  template  struct is_smart_pointer_helper : public std::false_type {}; template  struct is_smart_pointer_helper<std::shared_ptr > : public std::true_type {}; template  struct is_smart_pointer_helper<std::unique_ptr > : public std::true_type {}; template  struct is_smart_pointer_helper<std::weak_ptr > : public std::true_type {}; template  struct is_smart_pointer : public is_smart_pointer_helper<typename std::remove_cv::type> {}; template  typename std::enable_if<is_smart_pointer::value,void>::type check_smart_pointer(const T& t) { std::cout << "is smart pointer" << std::endl; } template  typename std::enable_if<!is_smart_pointer::value,void>::type check_smart_pointer(const T& t) { std::cout << "not smart pointer" << std::endl; } int main() { int* p(new int(2)); std::shared_ptr pp(new int(2)); std::unique_ptr upp(new int(4)); check_smart_pointer(p); check_smart_pointer(pp); check_smart_pointer(upp); return 0; }

程序输出:

not smart pointer
is smart pointer
is smart pointer

参考材料

http://www.cplusplus.com/reference/type_traits/enable_if/

https://en.cppreference.com/w/cpp/types/enable_if

总结

到此这篇关于C++11模板元编程-std::enable_if的文章就介绍到这了,更多相关C++11模板元编程-std::enable_if内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是C++11模板元编程-std::enable_if示例详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++11模板元编程-std::enable_if示例详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址