模板类结构与元函数

请看下面元函数_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 分钟前
从基础到实战-rmpt to webrtc
c++·webrtc·rtmp·流媒体
爱学习的白杨树13 分钟前
Sentinel介绍
java·开发语言
Frankabcdefgh19 分钟前
Python基础数据类型与运算符全面解析
开发语言·数据结构·python·面试
kaiaaaa23 分钟前
算法训练第十五天
开发语言·python·算法
Coovally AI模型快速验证38 分钟前
SLAM3R:基于单目视频的实时密集3D场景重建
神经网络·算法·3d·目标跟踪·音视频
whoarethenext1 小时前
使用 C/C++ 和 OpenCV 提取图像的感兴趣区域 (ROI)
c语言·c++·opencv
小玺玺1 小时前
[RDK X5] MJPG编解码开发实战:从官方API到OpenWanderary库的C++/Python实现
c++·python·opencv·rdk x5
Once_day1 小时前
代码训练LeetCode(29)最后一个单词的长度
算法·leetcode·c
凌肖战1 小时前
力扣上C语言编程题:最大子数组和(涉及数组)
c语言·算法·leetcode
蒟蒻小袁1 小时前
力扣面试150题--除法求值
算法·leetcode·面试