模板元编程 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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407923.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

6月,启蒙篇

6月&#xff0c;哈哈 在过去的4月与5月里我一直放荡着自己&#xff0c;我发现一个人没有目标与理想后是这样的无耐 &#xff0c;也是这样的无所做为。在那两个月里我放弃了所有的程序&#xff0c;我只是在计算机前玩游戏 。我原本以为这样会过得开心点&#xff0c;会让自己放松…

JS 计算日期天数差

function dayDiffer(startDate,endDate){console.info((endDate.getTime - startDate.getTime())/(24*60*60*1000));return Math.floor((endDate.getTime() - startDate.getTime())/(24*60*60*1000)); }转载于:https://www.cnblogs.com/jsczljh/p/3647395.html

自定义控件-实现TextBox的禁止粘贴

开发环境&#xff1a;Visual Studio .net 2005 Windows XP sp2 professional 新建->项目&#xff0d;>Windows控件库: 新建一个类&#xff0c;继承自TextBox类&#xff0c;具体源代码如下&#xff1a; using System;using System.Collections.Generic;using System.Comp…

模板元编程(二) Template Metaprogramming ---C++ 20

模板元编程(二) Template Metaprogramming —C 20 现在我们介绍参数与模板参数混合使用 先看一下例子: #include <iostream>int power(int m, int n) {int r 1;for (int k 1; k < n; k) r * m;return r; }template <int m, int n> struct Power {static in…

探索 ASP.NET Futures (Part 2 - Search Enabled)

在本系列的上一篇文章中&#xff0c;我们探索了ASP.NET Futures (May CTP)的SearchSiteMap功能&#xff0c;说明了如何将ASP.NET的SiteMap影射为符合Sitemaps协议的XML以便搜索引擎更好的抓取我们的站点。然而让搜索引擎更好的抓取我们的站点了&#xff0c;这部分的优化却仅仅对…

让窗体获得焦点,一定会有您用到的时候

开发环境&#xff1a;Visual Studio .NET 2005 下的Windows Form Application 应用场景: 当我们有个窗体中的数据发生了变化而此窗体又没有获得焦点(不是用户操作的当前窗口)的时候&#xff0c;我们希望它获得焦点&#xff0c;这样用户就可以立刻发现它上面的数据发生了变化。…

容器和算法的改进 --- C++20

容器和算法的改进 — C20 C 20对容器和算法有很多的改进 std::vector 和std::string支持constexpr所有容器支持consistent container erasure , contains新的算法移动元素 std::shift_left可以检查 std::string 的前缀和后缀 支持 constexpr 的容器和算法 C 20的std::vecto…

CodeSmith基础(二)

本文将介绍CodeSmith与数据库进行交互生成相应的存储过程&#xff0c;本例使用的数据库为SQL Server 2000。 在与数据库进行交互时&#xff0c;我们使用到了一个CodeSmith自带的组件SchemaExplorer&#xff0c;利用这个组件我们可以访问数据库的数据表、存储过程、视图等…

[Android]使用ViewPager实现图片滑动展示

在淘宝等电商的APP首页经常能看到大幅的广告位&#xff0c;通常有多幅经常更新的图片用于展示促销信息&#xff0c;如下图所示&#xff1a; 通常会自动滚动&#xff0c;也可以根据手势滑动。我没有研究过人家的APP是通过什么实现的&#xff0c;可能有第三方已经封装好的控件可以…

react(85)--error:Error creating bean with name ‘onlineStudyController‘:

Error creating bean with name onlineStudyController: Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)" 接口定义了同样的值

c#获取当前应用程序所在路径

一、获取当前文件的路径1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径&#xff0c;包括文件名。2. System.Environment.CurrentDirectory 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。3. System.IO.…

requires表达式 ---C++ 20 模板

requires表达式 —C 20 模板 requires还可以接一个表达式,该表达式也是一个纯右值表达式,表达式为true时满足约束条件,false则不满足约束条件 requires表达式的判定标准:对requires表达式进行模板实参的替换,如果替换之后出现无效类型,或者违反约束条件,则值为false,反之为tr…

读古诗系列--(两首)题都城南庄/江楼感旧

题都城南庄 唐 崔护去年今日此门中&#xff0c; 人面桃花相映红。 人面不知何处去&#xff0c; 桃花依旧笑春风。 江楼感旧 唐 赵嘏 独上江楼思渺然&#xff0c;月光如水水如天。 同来望月人何在&#xff1f;风景依稀似去年。前为课本所学&#xff0c;皆知&#xff…

react(86)--列表项控制选中

if (newarr.length 0) {message.error(请选中列表内容);return false;}

CheckBoxList 全选(jquery版本)

function selectedAll(allselect, obj) {$("#"obj.id" input:checkbox").each(function () {if (allselect.checked)$(this).attr("checked", true);else {$(this).attr("checked", false);}});} 调用 <input id"allCheck&quo…

如何输出源文件的标题和目前执行行的行数

环境&#xff1a;VC6.0 C版本&#xff1a; #include<iostream>using namespace std; void main(){ int line __LINE__; //注意&#xff1a;LINE前后分别是两个下划线“-”(半角状态下) char * file __FILE__; cout<<line<<endl; cout<<file<&…

类型萃取类型比较 Type-Traits Librarytype comparisons --- C++20

类型萃取:类型比较 Type-Traits Library:type comparisons — C20 不涉及runtime只在编译期 Comparing types 类型比较 C11 支持三种类型: is_base_of<Base, Derived>is_convertible<From, To>is_same<T, U> C20新加了几种: is_pointer_interconvertibl…

SBO部分SQL查询奉献

产品生产主计划SELECT T0.MsnCode AS 计划单号, T1.ItemCode, T2.itemname AS 产品名称,T1.Quantity as 生产数量, T2.onhand,T2.onorder as 已下单,T1.StartDate, T1.EndDate, T1.BaseDocNum as 订单号,T1.BaseDue as 完工日期,T1.ParentCode as 产品名 FROM OMSN T0 INNER JO…