检查类里是否存在某种类型的几种方法,以检查xxx类型为例:
方法1:

template<class T>
class has_member_type_Type
{
struct big { char a[]; };
template<class C> static big probe(typename C::xxx*); // match here if type T::Type exists
template<class C> static char probe(...);
public:
static const bool value = sizeof(probe<T>(nullptr)) > ;
};

方法2:

template<typename T, typename = typename T::xxx>
static std::true_type has_xxx_impl(int); template<typename T>
static std::false_type has_xxx_impl(...); template<typename T>
struct has_xxx : decltype(has_xxx_impl<T>())
{
};

方法3:

template<typename... Ts> struct make_void { typedef void type; };
template<typename... Ts> using void_t = typename make_void<Ts...>::type; template<typename T, typename = void>
struct has_yyy : std::false_type
{
}; template<typename T>
struct has_yyy < T, void_t<typename T::xxx>> : std::true_type
{
}; //用宏简化
#define HAS_TYPE_MEMBER(MEMBER)\
template<typename T, typename = void>\
struct has_type_##MEMBER : std::false_type\
{\
};\
template<typename T>\
struct has_type_##MEMBER < T, void_t<typename T::MEMBER>>:\
std::true_type\
{\
}; HAS_TYPE_MEMBER(xxx)

测试代码:

struct MyStruct
{
typedef char xxx;
}; int main()
{
static_assert(has_xxx<MyStruct>::value, "no xxx");
static_assert(has_type_xxx<MyStruct>::value, "no xxx");
}

CHECK MEMBER TYPE的更多相关文章

  1. how to check SVG type in js

    how to check SVG type in js SVGSVGElement & SVGElement svg = document.querySelector(`svg`); // & ...

  2. check member function

    template<typename T> struct has_member_foo11 { private: template<typename U> static auto ...

  3. How To Check Member In Window VS With CplusPlus?

    实例说明 下面这个实例代码, 快速举例了在Win32应用程序下,对于内存的泄漏检查. 其中的原理,目前本人还是不太的理解. 只是记录了使用方法. 以后,看情况,会更新的. #ifdef _WIN32 ...

  4. 最新版自动检测卡片类型工具软件版本(auto check card type v3.2.0)

    自动检测卡片类型工具软件. 卡片放到读卡器上面自动识别卡片类型,不需老是按下按钮,好用,方便.支持自动识别NTAG213卡片,NTAG215卡片, NTAG216卡片,Ultralight芯片, Ul ...

  5. list_entry(ptr, type, member)——知道结构体内某一成员变量地址,求结构体地址

    #define list_entry(ptr, type, member) \ ((type *)(() -> member))) 解释: 1 在0这个地址看做有一个虚拟的type类型的变量,那 ...

  6. 对list_entry(ptr, type, member)的理解

    如何根据一个结构体成员的地址.结构体类型以及该结构体成员名获得该结构体的首地址? #define list_entry(ptr, type, member) \ ((type *)((char *)( ...

  7. reifiable type与raw type

    下面的逻辑需要明白如下两个概念: 4.7. Reifiable Types 4.8. Raw Types 举几个是Reifiable Types的例子,如下: class A{} class B< ...

  8. Welcome-to-Swift-18类型转换(Type Casting)

    类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...

  9. [TypeScript] Generic Functions, class, Type Inference and Generics

    Generic Fucntion: For example we have a set of data and an function: interface HasName { name: strin ...

随机推荐

  1. 重启EBS

    http://www.cnblogs.com/toowang/archive/2012/03/28/2421275.html 概述步骤(此操作没有停止数据库): 1.登陆FTP,终止应用:cd $CO ...

  2. 轻易实现基于linux或win运行的聊天服务端程序

    对于不了解网络编程的开发人员来说,编写一个良好的服务端通讯程序是一件比较麻烦的事情.然而通过EC这个免费组件你可以非常简单地构建一个基于linux或win部署运行的网络服务程序.这种便利性完全得益于m ...

  3. 用c#开发微信 (17) 微活动 3 投票活动 (文本投票)

    前面介绍了微活动<大转盘> 和 <刮刮卡>,这次介绍下微投票,微投票分二种,一种是文本投票, 一种是图片投票.   下面介绍文本投票的详细步骤: 1. 新建文本投票活动     ...

  4. Mac上远程桌面连接Windows Server 2012 R2

    在将一台服务器的操作系统由Windows Server 2012升级为Windows Server 2012 R2之后,在Mac电脑上用微软的远程桌面软件怎么也连不上服务器,错误信息如下: Remot ...

  5. Swift 笔记

    苹果官方文档 https://developer.apple.com CocoaChina帮助文档 http://www.cocoachina.com/special/swift/ 74个Swift标 ...

  6. UML类图基本元素符号

     UML类图基本元素符号 元素名称 符号图例 含义 Class 包含类的名称.属性和方法定义. 可见性修饰符: + public -  private # protected 无修饰符为 intern ...

  7. Kali Linux系列教程之OpenVas安装

    Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1.  服务器层组件 2.客户层组件 安装过程 Initial setup ...

  8. Microsoft Azure开发体验 – 网络报名系统

    去年底RP好抢到了中国版Azure的使用机会,最近社团里讨论到9月份招新的问题,就用Azure Website和Azure Table Storage打造了这个报名系统. 网站放在 http://jo ...

  9. lucene索引文件大小优化小结

    http://www.cnblogs.com/LBSer/p/4068864.html 随着业务快速发展,基于lucene的索引文件zip压缩后也接近了GB量级,而保持索引文件大小为一个可以接受的范围 ...

  10. jQuery实现左移右移

    <html> <head> <meta charset="utf-8"> <title>完成左移右移</title> & ...