[C + + STL (VS2012 Update4) source code reading Series (3)] the various combinations of template function pointers and how they are invoked

Source: Internet
Author: User

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 (&AMP;FR) (const std::string&,const std::string&);
Template class template member function pointer
typedef const STD::STRING (_BIND_A&LT;STD::STRING,STD::STRING&GT;::* CFP) (std::string&);
A reference to a template class template member function pointer that cannot be defined
typedef const STD::STRING (_BIND_A&LT;STD::STRING,STD::STRING&GT;::&AMP;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 = (&AMP;BIND_A-&GT;*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 ...

*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.