c++元编程tookit类

前言

下面记录几个,在c++元编程中常用的工具类。

Hollow Types

instance_of

代码

c 复制代码
struct instance_of
{
	typedef T type;
	instance_of(int = 0)
	{
	}
};

const instance_of<int> I_INT = instance_of<int>(); // 这种写法有点繁琐
const instance_of<double> I_DOUBLE = 0; // 使用这种写法
// also fine.

作用

快速创建一个global对象,立刻初始化。

empty

代码

c 复制代码
struct empty
{
	empty() {}
};
// const empty EMPTY; 这种写法可能会报 unused的警告
const empty EMPTY = 0;

作用

用该类表示空。

Selector

代码

c 复制代码
template <bool PARAMETER>
struct selector
{
};
typedef selector<true> true_type1;
typedef selector<false> false_type;

作用

可以作为类型参数来做偏特化选择。

Static Value

代码

c 复制代码
template <typename T, T VALUE>
struct static_parameter
{
};
template <typename T, T VALUE>
struct static_value : static_parameter<T, VALUE>
{
	static const T value = VALUE;
	operator T () const
	{
	return VALUE;
	}
	static_value(int = 0)
	{
	}
};

//将static转为非static ,更安全
template <typename T, T VALUE>
inline T static_value_cast(static_value<T, VALUE>) //不需要用到形参,所以可以不用定义形参名
{
return VALUE;
};


int main() {
    auto a  =  static_value_cast(static_value<int, 3>());
    std::cout <<  a << "\n";
}

作用

快速创建一个static对象

相关推荐
Evand J几秒前
【MATLAB程序,一维非线性EKF与RTS】MATLAB,用于一维的位移与速度滤波和RTS平滑/高精度定位,带滤波前后的误差对比
开发语言·matlab·卡尔曼滤波·rts平滑·正向滤波
火云洞红孩儿5 小时前
告别界面孤岛:PyMe如何用一站式流程重塑Python GUI开发?
开发语言·python
叫我辉哥e15 小时前
新手进阶Python:办公看板集成ERP跨系统同步+自动备份+AI异常复盘
开发语言·人工智能·python
小丑西瓜6665 小时前
CMake基础用法,cmake_minimum_required,project,add_executable
linux·服务器·c++·camke
晚风吹长发6 小时前
初步了解Linux中的命名管道及简单应用和简单日志
linux·运维·服务器·开发语言·数据结构·c++·算法
fpcc6 小时前
设计心得——隔离隐藏的初步实践
c++
C++ 老炮儿的技术栈6 小时前
不调用C++/C的字符串库函数,编写函数strcpy
c语言·开发语言·c++·windows·git·postman·visual studio
布局呆星6 小时前
闭包与装饰器
开发语言·python
fyzy6 小时前
C++写后端实现,实现前后端分离
开发语言·c++
huohuopro6 小时前
Mybatis的七种传参方式
java·开发语言·mybatis