模板类结构与元函数

请看下面元函数_if:

template <bool B, typename L, typename R>

struct _if
{
typedef R type;
};

template <typename L, typename R>

struct _if<true, L, R>
{
typedef L type;
};

执行以下代码(1):

cpp 复制代码
#include<iostream>

/// <summary>
/// _if结构
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
/// <typeparam name="B"></typeparam>
template <bool B, typename L, typename R>
struct _if
{
	typedef R type;

	_if()
	{
		std::cout << "结构1" << "\n";
	}
};

 

void main()
{
	//std::string var1 = "text"
	_if<true, int, std::string>::type var1 = "text";

	_if<true, int, std::string> a1;

}

输出:

现在上面代码中再加一个_if结构2

/// <summary>

/// _if结构2
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
template <typename L, typename R>
struct _if<true, L, R>
{
typedef L type;
};

cpp 复制代码
/// <summary>
/// _if结构
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
/// <typeparam name="B"></typeparam>
template <bool B, typename L, typename R>
struct _if
{
	typedef R type;

	_if()
	{
		std::cout << "结构1" << "\n";
	}
};

/// <summary>
/// i_if结构2
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
template <typename L, typename R>
struct _if<true, L, R>
{
	typedef L type;
};




void main()
{
	//std::string var1 = "text"
	_if<true, int, std::string>::type var1 = "text";

	_if<true, int, std::string> a1;

}

执行结果:

为什么编译不过,因为现在用的是结构:

也就是说:_if<true, int, std::string>::type var1 => int (返回整型),修改代码:

cpp 复制代码
#include<iostream>

/// <summary>
/// _if结构
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
/// <typeparam name="B"></typeparam>
template <bool B, typename L, typename R>
struct _if
{
	typedef R type;

	_if()
	{
		std::cout << "结构1" << "\n";
	}
};

/// <summary>
/// i_if结构2
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="R"></typeparam>
template <typename L, typename R>
struct _if<true, L, R>
{
	typedef L type;
	_if()
	{
		std::cout << "结构2" << "\n";
	}
};




void main()
{ 
	_if<true, int, std::string>::type var1 = 5;
	_if<false, int, std::string>::type var2 = "text";

	_if<true, int, std::string> a1;
	_if<false, int, std::string> a2;
}

输出:

这就是元函数_if原理:

_if 原函数:

cpp 复制代码
template <bool B, typename L, typename R>
struct _if{	typedef R type;};

template <typename L, typename R>
struct _if<true, L, R>{ 	typedef L type;};
相关推荐
小声读源码11 分钟前
【技巧】使用UV创建python项目的开发环境
开发语言·python·uv·dify
yxc_inspire18 分钟前
基于Qt的app开发第七天
开发语言·c++·qt·app
zm-v-1593043398620 分钟前
解锁遥感数据密码:DeepSeek、Python 与 OpenCV 的协同之力
开发语言·python·opencv
冲帕Chompa21 分钟前
图论part09dijkstra算法
算法·图论
·云扬·30 分钟前
【PmHub后端篇】PmHub中基于Redis加Lua脚本的计数器算法限流实现
redis·算法·lua
周Echo周32 分钟前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
zkmall36 分钟前
推荐算法工程化:ZKmall模板商城的B2C 商城的用户分层推荐策略
算法·机器学习·推荐算法
明天更新37 分钟前
Java处理压缩文件的两种方式!!!!
java·开发语言·7-zip
老胖闲聊43 分钟前
C# 注册表操作类
开发语言·c#
勘察加熊人1 小时前
Python+Streamlit实现登录页
开发语言·python