[C++ 2011 STL (VS2012 Update4) 源代码阅读系列(3)]模板函数指针的的各种组合以及调用的方式
2013-08-12 16:50
597 查看
// function.cpp :
/*
测试例子演示了函数指针的的各种组合以及调用的方式
如果你的编译器编译失败,请尽可能的先去掉 const 后编译,如果全部去掉后
还是编译失败,请把你的编译器升级到最高版本或者换了它。
*/
#include "stdafx.h"
#include "stdafx.h"
#include <functional>
#include <string>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
//for _1, _2, _3...
using namespace std::placeholders;
template<class T1,class T2> const T1 my_strcat(const T1 &t1,const T2 &t2)
{
return std::move(t1 + t2);
}
struct Null_class{};
template<class _Fun,class v0_t = Null_class,class v1_t = Null_class,class = Null_class> class _Bind_A;
template<class _Fun,class v0_t> class _Bind_A<_Fun,v0_t>
{
private:
_Fun m_v;
public:
_Bind_A(_Fun v):m_v(v)
{
//
}
const std::string my_strcat(v0_t &v)
{
return m_v + v;
}
};
void test_bind()
{
std::string s1 = "aaa",s2 = "BBB",ss = "";
ss = std::bind(my_strcat<std::string,std::string>,s1,s2)();
std::cout<<ss<<endl;
/*
函数类型 (*指针变量名)(形参列表);“函数类型”说明函数的返回类型, 由于“()”的优先
级高于“*”,所以指针变量名外的括号必不可少;如果是类成员函数的指针,*前面需要加
类名::限定范围,类似这样:函数类型 (类名::*指针变量名)(形参列表);如果是模版类,
还需要指明模版参数类型;
*/
//全局模板函数指针
typedef const std::string (*FP)(const std::string&,const std::string&);
//全局模板函数指针的引用
typedef const std::string (&FR)(const std::string&,const std::string&);
//模板类模板成员函数指针
typedef const std::string (_Bind_A<std::string,std::string>::*CFP)(std::string&);
//模板类模板成员函数指针的引用,无法定义
//typedef const std::string (_Bind_A<std::string,std::string>::&CFR)(const std::string&);
FP fp = my_strcat<std::string,std::string>;
ss = fp(s1,s2);
std::cout<<ss<<endl;
fp = &my_strcat<std::string,std::string>;
ss = fp(s1,s2);
std::cout<<ss<<endl;
FR fr = my_strcat<std::string,std::string>;
ss = fr(s1,s2);
std::cout<<ss<<endl;
_Bind_A<std::string,std::string> bind_a(s1);
//c++ 2011 用 nullptr 代替指向空的指针
CFP cfp = nullptr;
//CFP cfp = 0;
cfp = &_Bind_A<std::string,std::string>::my_strcat;
//通过.调用
ss = (bind_a.*cfp)(s2);
std::cout<<ss<<endl;
//通过->调用
ss = (&bind_a->*cfp)(s2);
std::cout<<ss<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
test_bind();
system("pause");
return 0;
}
/*
输出:
aaaBBB
aaaBBB
aaaBBB
aaaBBB
aaaBBB
aaaBBB
请按任意键继续. . .
*/
/*
测试例子演示了函数指针的的各种组合以及调用的方式
如果你的编译器编译失败,请尽可能的先去掉 const 后编译,如果全部去掉后
还是编译失败,请把你的编译器升级到最高版本或者换了它。
*/
#include "stdafx.h"
#include "stdafx.h"
#include <functional>
#include <string>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
//for _1, _2, _3...
using namespace std::placeholders;
template<class T1,class T2> const T1 my_strcat(const T1 &t1,const T2 &t2)
{
return std::move(t1 + t2);
}
struct Null_class{};
template<class _Fun,class v0_t = Null_class,class v1_t = Null_class,class = Null_class> class _Bind_A;
template<class _Fun,class v0_t> class _Bind_A<_Fun,v0_t>
{
private:
_Fun m_v;
public:
_Bind_A(_Fun v):m_v(v)
{
//
}
const std::string my_strcat(v0_t &v)
{
return m_v + v;
}
};
void test_bind()
{
std::string s1 = "aaa",s2 = "BBB",ss = "";
ss = std::bind(my_strcat<std::string,std::string>,s1,s2)();
std::cout<<ss<<endl;
/*
函数类型 (*指针变量名)(形参列表);“函数类型”说明函数的返回类型, 由于“()”的优先
级高于“*”,所以指针变量名外的括号必不可少;如果是类成员函数的指针,*前面需要加
类名::限定范围,类似这样:函数类型 (类名::*指针变量名)(形参列表);如果是模版类,
还需要指明模版参数类型;
*/
//全局模板函数指针
typedef const std::string (*FP)(const std::string&,const std::string&);
//全局模板函数指针的引用
typedef const std::string (&FR)(const std::string&,const std::string&);
//模板类模板成员函数指针
typedef const std::string (_Bind_A<std::string,std::string>::*CFP)(std::string&);
//模板类模板成员函数指针的引用,无法定义
//typedef const std::string (_Bind_A<std::string,std::string>::&CFR)(const std::string&);
FP fp = my_strcat<std::string,std::string>;
ss = fp(s1,s2);
std::cout<<ss<<endl;
fp = &my_strcat<std::string,std::string>;
ss = fp(s1,s2);
std::cout<<ss<<endl;
FR fr = my_strcat<std::string,std::string>;
ss = fr(s1,s2);
std::cout<<ss<<endl;
_Bind_A<std::string,std::string> bind_a(s1);
//c++ 2011 用 nullptr 代替指向空的指针
CFP cfp = nullptr;
//CFP cfp = 0;
cfp = &_Bind_A<std::string,std::string>::my_strcat;
//通过.调用
ss = (bind_a.*cfp)(s2);
std::cout<<ss<<endl;
//通过->调用
ss = (&bind_a->*cfp)(s2);
std::cout<<ss<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
test_bind();
system("pause");
return 0;
}
/*
输出:
aaaBBB
aaaBBB
aaaBBB
aaaBBB
aaaBBB
aaaBBB
请按任意键继续. . .
*/
相关文章推荐
- [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化
- [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化
- [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(1)]准备知识
- 深度探索C++对象模型---Function语义学之Member的各种调用方式
- C/C++:函数的编译方式与调用约定以及extern “C”的使用
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C/C++ 函数的编译方式与调用约定以及extern “C”的使用
- 关于C++组合理解以及拷贝构造函数的调用时间的理解
- C++的引用与const指针的关系以及各种传递方式
- C/C++:函数的编译方式与调用约定以及extern “C”的使用
- 二叉树系列——二叉树的定义以及各种遍历方式
- C++对象模型——Member的各种调用方式(第四章)
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C++:浅谈c++资源管理以及对[STL]智能指针auto_ptr源码分析,左值与右值
- sizeof求struct各种组合的长度,以及指针,字符数组的长度
- STL容器插入类时的构造函数调用以及插入指针时的情况
- C/C++:函数的编译方式与调用约定以及extern “C”的使用
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
- 指定结构体排序字段的STL实现版本(C++ 2011系列)