Function.cpp:
/*
The test example illustrates the various combinations of function pointers and how they are invoked
If your compiler failed to compile, please remove the const as much as possible before compiling, if all is removed
or failed to compile, please upgrade your compiler to the highest version or change it.
*/
#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::p laceholders;
Template<class t1,class t2> const T1 my_strcat (const T1, &t1,const 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;
/*
function type (* pointer variable name) (parameter list); "Function Type" describes the return type of the function because the precedence of "()"
Level is higher than "*", so the parentheses outside the pointer variable name are necessary, and if you are a pointer to a class member function, you need to precede the
Class Name:: Scoped, like this: function type (class name::* pointer variable name) (parameter list), if it is a template class,
You also need to specify the template parameter type;
*/
Global template function pointer
typedef const STD::STRING (*FP) (const std::string&,const std::string&);
References to global template function pointers
typedef const STD::STRING (&FR) (const std::string&,const std::string&);
Template class template member function pointer
typedef const STD::STRING (_BIND_A<STD::STRING,STD::STRING>::* CFP) (std::string&);
A reference to a template class template member function pointer that cannot be defined
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 with nullptr instead of pointing to null pointer
CFP CFP = nullptr;
CFP CFP = 0;
CFP = &_Bind_A<std::string,std::string>::my_strcat;
Pass. Call
SS = (BIND_A.*CFP) (S2);
std::cout<<ss<<endl;
Called by->
SS = (&BIND_A->*CFP) (S2);
std::cout<<ss<<endl;
}
int _tmain (int argc, _tchar* argv[])
{
Test_bind ();
System ("pause");
return 0;
}
/*
Output:
aaabbb
aaabbb
aaabbb
aaabbb
aaabbb
aaabbb
Please press any key to continue ...
*/