模板类结构与元函数

请看下面元函数_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;};
相关推荐
爱丫爱几秒前
Python中常见库 PyTorch和Pydantic 讲解
开发语言·pytorch·python
Ryan_Gosling2 分钟前
C++-构造函数-接口
开发语言·c++
ceffans11 分钟前
PDF文档中文本解析
c++·windows·pdf
SummerGao.18 分钟前
Windows 快速搭建C++开发环境,安装C++、CMake、QT、Visual Studio、Setup Factory
c++·windows·qt·cmake·visual studio·setup factory
查理零世21 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
仟濹22 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
紫雾凌寒31 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
服务端相声演员1 小时前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
YH_DevJourney1 小时前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++
京东零售技术1 小时前
AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践
算法