When it comes to traits, the policy will be contacted accordingly. So what does policy do?
Take a look at the following cumulative code.
Template<class T, class P>typename traits<t>::accut accum (const t* ptr, int len) {traits<t>::accut Total = Traits<t>::zero (), for (int i = 0, i < len; i++) {Total + = * (ptr + i);} return total;}
Note Total + = * (ptr + i); this line. Here are two questions:
1. Not all types support the + = operator, if a custom type is passed in, and the + = is not defined, what should I do?
2. If you do not want to use + =, such as passing in a string, and then want to reverse stitching.
To solve these 2 problems, the first thought is that this place should not hard code, by passing in the parameters of the method to dynamically modify this line of code.
There are generally two ways to do this:
1. Pass in a function object, and then call the corresponding function through the function object
2. Use the policy class.
The second method is described here: Policy
We change the additive function to:
Template<class T, class P>typename traits<t>::accut accum (const t* ptr, int len) {traits<t>::accut Total = Traits<t>::zero (); for (int i = 0; i < len; i++) {p::accumulate (total, * (ptr + i));} return total;}
Note that we add a template parameter P and then call P::accumulate (Total, * (ptr + i));
Now let's define this P class:
Class P{public:template<class T1, Class t2>static void accumulate (t1& total, T2 v) {total + = V;}};
There is only one function in the P class, which is a static template function.
In this way, we can call the following:
int sz[] = {1, 2, 3};traits<int>::accut avg = accum<int, p> (SZ, 3)/3;
Get results 2.
So what if I'm going to pass in a character array now and want to reverse-stitch it?
First, change the char traits return type to string:
Template<>struct traits<char>{typedef std::string accut;static accut Zero () {return std::string ();};};
Then add a policy class:
Class P2{public:template<class T1, Class t2>static void accumulate (t1& total, T2 v) {total.insert (0, 1, v);}};
A call will find that the return value is CBA.
Char str[] = {' A ', ' B ', ' C '};auto ret = Accum<char, p2> (str, 3);
If you call auto ret = accum<char,P> (str, 3), it will return ABC because the string itself also supports + =, all P:: The accumulate can run normally and is sequential stitching.
This is the use of policy, which allows us to replace the additive algorithm inside the additive function, which is very flexible.
Full code:
ConsoleApplication1.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <memory& GT, #include <algorithm> #include <numeric> #include <string>template<typename t>struct traits ; template<>struct traits<char>{typedef std::string accut;static accut Zero () {return std::string ();};}; Template<>struct traits<int>{typedef Double accut;static accut Zero () {return 0.0;};}; Template<class T, class P>typename traits<t>::accut accum (const t* ptr, int len) {traits<t>::accut Total = Traits<t>::zero (); for (int i = 0; i < len; i++) {p::accumulate (total, * (ptr + i));} return total;} Class P{public:template<class T1, Class t2>static void accumulate (t1& total, T2 v) {total + = V;}}; Class P2{public:template<class T1, Class t2>static void accumulate (t1& total, T2 v) {total.insert (0, 1, v);}}; int _tmain (int argc, _tchar* argv[]) {int sz[] = {1, 2, 3};traits<int>::accut avg = AccuM<int, P> (SZ, 3)/3;char str[] = {' A ', ' B ', ' C '};auto ret = Accum<char, p> (str, 3); ret = Accum<char, P2> ;(str, 3); return 0;}
C + + templates-policy class