模板元编程 Template Metaprogramming--- C++ 20

模板元编程(一) Template Metaprogramming— C++ 20

在编译期进行类型操作

举个例子:

std::move在概念上应该这样实现(实际并不是这么做的):

static_cast<std::remove_reference<decltype(arg)>::type&&>(arg);

意义上,std::move首先获取它的参数arg,推断出其类型,移除引用,最后转换为右值引用,移动语义就生效了.

如何移除参数的const呢?

#include <iostream>
#include <type_traits>

template <typename T>
struct removeConst
{
	using type = T; // (1)
};

template <typename T>
struct removeConst<const T>
{
	using type = T; // (2)
};

int main()
{
	std::cout << std::boolalpha;
	std::cout << std::is_same_v<int, removeConst<int>::type> << '\n'; // true    
	std::cout << std::is_same_v<int, removeConst<const int>::type> << '\n'; // true
}

std::is_same_v<int, removeConst<int>::type> 等同std::is_same<int, removeConst<int>::type>::value,其中::type表示编译器推断出来的模板类型

传入int时,应用removeConst

传入const int,应用removeConst<const T>,这样就移除了const,很好理解

Metadata

metadata是编译阶段元函数使用的数据

一共有三种类型:

  • 类型参数:int,double
  • 非类型参数,例如:数字类型,枚举类型…
  • 模板,例如std::vector

以后会详细解释这部分内容

Metafunctions

元函数是在编译期执行的函数

元函数:

template <int a , int b>
struct Product {
    static int const value = a * b;
};

template<typename T >
struct removeConst<const T> {
    using type = T;
};

函数 vs 元函数

#include <iostream>

int power(const int m, const int n)
{
	int r = 1;
	for (int k = 1; k <= n; ++k) r *= m;
	return r;
}

template <int M, int N>
struct Power
{
	static int const value = M * Power<M, N - 1>::value;
};


template <int M>
struct Power<M, 0>
{
	static int constexpr value = 1;
};

int main()
{
	std::cout << '\n';

	std::cout << "power(2, 10)= " << power(2, 10) << '\n';
	std::cout << "Power<2,10>::value= " << Power<2, 10>::value << '\n';

	std::cout << '\n';
}

image-20220522212804860

  • 参数:函数的参数在圆括号里面(...),元函数的参数在尖括号里面<...>

  • 返回值:函数返回一个语句,元函数返回一个静态常量值

    以后会介绍constexprconsteval

混合编程 Hybrid Programming

例子:

#include <iostream>

template <int n>
int Power(int m)
{
	return m * Power<n - 1>(m);
}

template <>
int Power<0>(int m)
{
	return 1;
}

int main()
{
	std::cout << '\n';

	std::cout << "Power<0>(10): " << Power<0>(20) << '\n';
	std::cout << "Power<1>(10): " << Power<1>(10) << '\n';
	std::cout << "Power<2>(10): " << Power<2>(10) << '\n';


	std::cout << '\n';
}

下一篇文章介绍


::cout << "Power<1>(10): " << Power<1>(10) << ‘\n’;
std::cout << "Power<2>(10): " << Power<2>(10) << ‘\n’;

std::cout << '\n';

}


下一篇文章介绍

------

[What does “::value, ::type” mean in C++? - Quora](https://www.quora.com/What-does-value-type-mean-in-C)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值