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 ...
随机推荐
- 如何用 Postman 处理 json请求格式
下边是其他博友写的 http://blog.163.com/huan12_8/blog/static/130519090201611711213719/
- 解决谷歌浏览器频繁出现adobe flash player因过期而遭到阻止的问题(转自知乎)
作者:在战争中链接:https://www.zhihu.com/question/32223811/answer/128088278来源:知乎著作权归作者所有,转载请联系作者获得授权. 很多新用户在安 ...
- Idea设置类注释模板
1.选择File–>Settings–>Editor–>File and Code Templates–>Includes–>File Header. 在Descript ...
- Lattice Constants and Crystal Structures of some Semiconductors
Lattice Constants and Crystal Structures of some Semiconductors and Other Materials Element or Compo ...
- 2018年全国多校算法寒假训练营练习比赛(第四场)F:Call to your teacher
传送门:https://www.nowcoder.net/acm/contest/76/F 题目描述 从实验室出来后,你忽然发现你居然把自己的电脑落在了实验室里,但是实验室的老师已经把大门锁上了.更糟 ...
- linux同步机制
很早之前就接触过同步这个概念了,但是一直都很模糊,没有深入地学习了解过,近期有时间了,就花时间研习了一下<linux内核标准教程>和<深入linux设备驱动程序内核机制>这两本 ...
- 【laravel VS lumen】
读取项目的配置信息 读取config文件database.php中的default属性信息 laravel:config('database.default'); lumen:app()->co ...
- 项目打包 TestFlight用法
TestFlight用法 包教包会(iOS APP官方测试工具) https://www.jianshu.com/p/4be185e4069c
- 34-ssm 最简洁的模板
见 csdn :https://download.csdn.net/download/qq_39451578/10931448
- 使用SQL语句创建数据库2——创建多个数据库文件和多个日志文件
在matser数据库下新建查询,输入的命令如下: USE master GOCREATE DATABASE E_MarketON PRIMARY--主文件组( NAME ='E_Market_data ...