c++11 template 模板练习
直接上代码吧
to do
// 111111.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <map>
#include <sstream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <type_traits> using namespace std; void sample1()
{
//基本的type trait
int i = ; if (true == std::is_void<int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
} if (true == std::is_const<int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
} if (true == std::is_const<int* const>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
} if (true == std::is_same<int, signed int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
}
} void sample2()
{
//从 remove_cv remove_reference 到 decay
// const volatile int(remove_cv)--> int == signed int
if (true == std::is_same< std::remove_cv<const volatile int>::type, signed int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
} // const volatile int&(remove_reference remove_cv )--> int == signed int
// equal std::decay
if (true == std::is_same< std::remove_cv< std::remove_reference< const volatile int&>::type>::type, signed int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
} // const volatile int&(remove_reference remove_cv )--> int == signed int
// equal std::decay<>
if (true == std::is_same< std::decay< const volatile int&>::type, signed int>::value) {
std::cout << true << std::endl;
}
else {
std::cout << false << std::endl;
}
}
//===============================================================
// std::decay 还可以用于将函数退化成函数指针 保存 用于延时执行 template<typename F>
struct SimpleFunction {
using FnType = typename std::decay<F>::type;
SimpleFunction(F f) :m_fn(f) {}
void run() { m_fn(); }
FnType m_fn;
}; void SimpFunctiontest() {
std::cout << __FUNCTION__ << std::endl;
} void sample3()
{
SimpleFunction<void()> f(SimpFunctiontest);
f.run();
}
//===================================================
template<typename T>
typename std::enable_if<std::is_same<T, int>::value, T>::type
foo(T t) {
return t;
} void sample4()
{
//enable_if
int i = ;
foo(i); //编译通过
char b = 'x';
//foo(b) //编译无法通过 因为 b的type不是int
}
//==============================================================
class Person {
public:
Person(std::string s) :name(s) {}
std::string name;
}; std::string funtion(Person p) {
return p.name;
} template<typename Fn>
multimap<typename std::result_of<Fn(Person)>::type,Person>
GroupBy(const vector<Person>& vt, Fn&& keySelector)
{
typedef typename std::result_of<Fn(Person)>::type key_type;
multimap<key_type, Person> map;
std::for_each(vt.begin(), vt.end(), [&](const Person& person)
{
map.insert(make_pair(keySelector(person), person));
});
return map;
} void sample5() {
// result_of 获取函数返回值类型
vector<Person> vp;
vp.push_back(Person(""));
vp.push_back(Person(""));
vp.push_back(Person(""));
GroupBy(vp, funtion);
}
//==============================================================
template<typename T>
typename std::enable_if<std::is_arithmetic<T>::value,int>::type foo1(T t)
{
std::cout << t << std::endl;
return ;
} template<typename T>
typename std::enable_if<!std::is_arithmetic<T>::value, int>::type foo1(T t)
{
std::cout << typeid(T).name() << std::endl;
return ;
} void sample6()
{
//enable_if
int i = ;
std::string b = "123ffdh";
foo1(i);
foo1(b);
}
//==========================================================================================
template<typename T>
typename std::enable_if<std::is_arithmetic<T>::value,std::string>::type
MyToString(T& t) { return std::to_string(t); } template<typename T>
typename std::enable_if<std::is_same<T,std::string>::value,std::string>::type
MyToString(T& t) { return t; } void sample7()
{
//enable_if示例
int i = ;
MyToString(i);
double d = 123.213;
MyToString(d);
std::string s = "zxd12";
MyToString(s);
}
//===============================================================
template<std::size_t I =,typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value>::type printtp(std::string s,const Tuple& t){
std::cout << std::endl <<s << std::endl;
} template<std::size_t I = , typename Tuple>
typename std::enable_if<I < std::tuple_size<Tuple>::value>::type printtp(std::string& s,const Tuple& t) {
std::cout << std::get<I>(t) << std::endl;
s += std::to_string(I);
s += " ";
printtp<I + >(s,t);
} template<typename ... Args>
void MyPinrt(Args... args)
{
std::string s;
printtp(s,std::make_tuple(args...));
} void sample8()
{
MyPinrt(,1.2324,false,"safda");
} //===========================================================================
int main()
{
/*sample1();
sample2();
sample3();
sample4();*/
//sample5();
//sample6();
sample7();
sample8();
return ;
}
c++11 template 模板练习的更多相关文章
- 编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异
编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异 题目挺绕口的.C++ 11的好东西不算太多,但变参模板(Variadic Template)肯定是其中耀眼的一 ...
- 微信小程序新闻列表功能(读取文件、template模板使用)
微信小程序新闻列表功能(读取文件.template) 不忘初心,方得始终.初心易得,始终难守. 在之前的项目基础上进行修改,实现读取文件内容作为新闻内容进行展示. 首先,修改 post.wxml 文件 ...
- c++11 函数模板的默认模板参数
c++11 函数模板的默认模板参数 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> ...
- django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...
- 【C/C++开发】C++11的模板类型判断——std::is_same和std::decay
C++11的模板类型判断--std::is_same和std::decay 问题提出:有一个模板函数,函数在处理int型和double型时需要进行特殊的处理,那么怎么在编译期知道传入的参数的数据类型是 ...
- angularjs指令系统系列课程(2):优先级priority,模板template,模板页templateUrl
今天我们先对 priority,template,templateUrl进行学习 1.priority 可取值:int 作用:优先级 一般priority默认为0,数值越大,优先级越高.当一个dom元 ...
- ArcGIS API for Silverlight代码中使用Template模板
原文:ArcGIS API for Silverlight代码中使用Template模板 在项目开发中,会遇到点选中聚焦闪烁效果,但是因为在使用Symbol的时候,会设置一定的OffSetX和OffS ...
- Git commit template 模板设定
多人协作开发一个项目时,版本控制工具是少不了的,git是linux 内核开发时引入的一个优秀代码管理工具,利用它能很好使团队协作完成一个项目.为了规范团队的代码提交,也方便出版本时的release n ...
- 一个简单地template模板
之前的项目中用到了artTemplate模板,感觉挺有意思,于是查看相关资料,自己动手写了个简单地template模板插件.虽然会有一些不足,但也是自己的一番心血.主体代码如下 /* * 一个简单地t ...
随机推荐
- 《java与模式》阅读笔记01
这次我读了前两章的内容,就如书名所言,这本书主要将的就是java中的模式,在书中的序言就把所有的模式都介绍了一下,主要有, 1.创建模式:简单工厂模式,工厂方法模式,抽象工厂模式,建造模式 2.行为模 ...
- Pandas基本功能之算术运算、排序和排名
算术运算和数据对齐 Series和DataFrame中行运算和列运算有种特征叫做广播 在将对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集.自动的数据对齐操作在不重叠的索引处引入了NA ...
- The hyperlink for cell A2 references relation rId1, but that didn't exist!
excel单元格中存在超链接,去掉 可以参考 https://www.cnblogs.com/songyunxinQQ529616136/p/6599523.html
- 第十一章 串 (b1)串匹配
- H3C S5800 MPLS----VPLS 三层路由透传二层网络
一.MPLS 介绍 多协议标签交换(Multi-Protocol Label Switching,MPLS)是新一代的IP高速骨干网络交换标准,由因特网工程任务组(Internet Engineeri ...
- git 基本操作命令
1. git status 查看git 状态 2.git init 3.git push -u origin master 提交 4.git remote set "邮箱地址i" ...
- gridview空间使用
1.HTML代码 <asp:GridView ID="gv_Info" runat="server" AutoGenerateColumns=" ...
- JavaScript各种继承方式(三):组合继承(combination inheritance)
一 原理 组合继承仅仅是同时使用了原型链继承和构造函数继承. 具体做法是,将父类的实例作为子类的构造函数的原型对象,并在子类的构造函数中调用父类的构造函数. function Fruit(name){ ...
- python------栈和队列的实现
一.神马是栈 古有粮仓,即为栈.粮仓的特点就是最后放进去的谷粒,放在最上面.打仗行军,取粮是怎么取最快?肯定是最先取最上面的!栈这一种常用到的数据结构就是这种特点:后进先出(Last In First ...
- swift - 全屏imageView的适配 - UIViewContentMode选择
1. /// 设置当前图片view大小 func setFrame(){ if #available(iOS 11.0, *) { if let y = JY_WINDOW?.safeAreaInse ...