CHECK MEMBER TYPE
检查类里是否存在某种类型的几种方法,以检查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的更多相关文章
- how to check SVG type in js
how to check SVG type in js SVGSVGElement & SVGElement svg = document.querySelector(`svg`); // & ...
- check member function
template<typename T> struct has_member_foo11 { private: template<typename U> static auto ...
- How To Check Member In Window VS With CplusPlus?
实例说明 下面这个实例代码, 快速举例了在Win32应用程序下,对于内存的泄漏检查. 其中的原理,目前本人还是不太的理解. 只是记录了使用方法. 以后,看情况,会更新的. #ifdef _WIN32 ...
- 最新版自动检测卡片类型工具软件版本(auto check card type v3.2.0)
自动检测卡片类型工具软件. 卡片放到读卡器上面自动识别卡片类型,不需老是按下按钮,好用,方便.支持自动识别NTAG213卡片,NTAG215卡片, NTAG216卡片,Ultralight芯片, Ul ...
- list_entry(ptr, type, member)——知道结构体内某一成员变量地址,求结构体地址
#define list_entry(ptr, type, member) \ ((type *)(() -> member))) 解释: 1 在0这个地址看做有一个虚拟的type类型的变量,那 ...
- 对list_entry(ptr, type, member)的理解
如何根据一个结构体成员的地址.结构体类型以及该结构体成员名获得该结构体的首地址? #define list_entry(ptr, type, member) \ ((type *)((char *)( ...
- reifiable type与raw type
下面的逻辑需要明白如下两个概念: 4.7. Reifiable Types 4.8. Raw Types 举几个是Reifiable Types的例子,如下: class A{} class B< ...
- Welcome-to-Swift-18类型转换(Type Casting)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...
- [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 ...
随机推荐
- Libgls 1.0.1 发布,OpenGL 立体渲染
Libgls 1.0.1 只是修复了一些小问题,更新了 FindGLS.cmake 脚本. Libgls 允许 OpenGL 立体渲染,不需要硬件支持四缓冲立体.支持许多立体显示模式,从立体眼镜的3D ...
- 基于Qt的流程设计器(一)
一: 先来看一下界面的截图: 说明: 拖动节点的时候,与该节点相关的箭头连线也会跟着调整: 用户可以使用鼠标从一个节点拖出一个箭头到另一个节点(鼠标在空白区域点击一下,拖出的箭头消失) 这三个 ...
- 为什么要设置getter和setter?
面向对象语言中,通常把属性设置为私有,然后添加getter和setter方法来访问.有人说,这本质上和设置属性为公有没有区别,干脆把属性public算了.也有人反驳,这样做破坏了封装.但是,破坏了封装 ...
- Java程序员的日常——经验贴(纯干货)二
继昨天的经验贴,今天的工作又收获不少. windows下编辑器会给文件添加BOM 在windows的编辑器中,为了区分编码,通常会添加一个BOM标记.比如,记事本.nodepade++.sublime ...
- asp.net core中Microsoft.AspNet.Session的使用
1.通过nuget/修改project.json引用 2.引用存储session的媒介,如内存.数据库.redis等 "Microsoft.AspNetCore.Session": ...
- paip. dsl 编程语言优点以及 常见的dsl
paip. dsl 编程语言优点以及 常见的dsl 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...
- paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决
paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决 作者Attilax 艾龙, ...
- 为ubuntu操作系统增加root用户
1:当安装好虚拟机,安装好Ubuntu操作系统后,登陆的时候发现除了自己的设置的用户就是外来用户,其实Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号,但是如果每次使用root ...
- Reorder List
题目: Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...
- 开发ERP软件应该遵守的22条规则
总结一下做管理软件,有哪些项是经过检验的条款,必须遵守的. 界面篇 1 要保存用户的偏号(profile/favourite). ASP.NET 2.0引入此功能,当用户修改默认的控件的属性时,框架 ...