C + + Template programming implements Haskell's function pattern matching feature [figure]:
The great god Bartosz Milewski wrote an article in 2009, "What Does Haskell has to does with C + +?", using C + + to implement some of the features of Haskell's functional programming language. "Portal at the end of the text"
There is an example of this:
Code 1
1.template<int N>class Fact {
2.public:
- Staticconstint value = n * fact<n-1>::value;
4.};
6.template<>class fact<0>{//specialization for n = 0
7.public:
- Staticconstint value = 1;
9.};
Note: The original is used in the struct keyword, this is changed to class and added public
I guess you didn't read it. It doesn't matter, let's skip the above section of C + + template code with "scary syntax".
What do you want to do with the above example? In fact, it just wants to calculate the factorial of N.
If you have learned recursion in the C language, you should know the following recursive function for calculating factorial
Code 2
int fact (int n) {
if (0== N)
RETURN1; Answer to the 0-order question. 0! equals 1
Else
Return (n fact (n-1));//Problem reduction: N-order->n-1 order
}
Its effect is equal to the following code
Code 3
int fact2 (int n) {//factorial calculated with For loop
int p = 1;
for (int i=n; I >=1; i--)
P = i;
return p;
}
So what's the difference between the first code (CODE1) and the second code (CODE2)?
The difference is that Code1 is computed at compile time (by the compiler), and Code2 is calculated at run time (that is, when the code is running).
Now to explain Code1 (partly according to Bartosz Milewski)
Code 1
the/1th line of code declares a class template fact.
This template accepts a "non-type parameter" N,
n is an integer.
/
1.template<int N>class Fact {
2.public:
/ 3rd line of code declares a static integer constant
Member value. The value is used by the
The recursive template represents the
/
- Staticconstint value = n * fact<n-1>::value;
4.};
The 6th line of code is the "special" type template fact,
Which is explicitly given a type parameter.
The code of one instance of the class template, not the
Compiler generation.
Here, is given the parameter n for the 0 o'clock template
The fact code. This way, the compiler will no longer
Code when generating n=0 based on a class template fact
For template special, see the link at the end of the article
/
6.template<>class fact<0>{//specialization for n = 0
7.public:
- static const int value = 1;
- };
/ According to the C + + specification, template-specific code must be
After the template declaration is placed.
So the code above looks like it was processed first.
The problem of descending order from N order to n-1 order, and then give
A 0-step solution.
It's not like Code2. There are if/else in Code2,
It is therefore possible to adjust the reduced-order code to the 0-order solution code
Order of precedence (if conditions are changed, of course).
/
So this code that uses a template to calculate factorial (class?) How do you use it? As follows:
cout << "factorial of 0 =" << fact<0>::value << Endl;
where the C + + compiler will match the "Fact<0>::value" call to the most appropriate template code, that is, the 第6-9 line code in Code1.
What if I call with a non-0 parameter?
cout << "factorial of 8 =" << fact<8>::value << Endl;
where the C + + compiler will match the 第1-4 line code in Code1 for the "Fact<8>::value" call.
The front Blahblhaaaaaaaaaaaah told a lot of, actually is not business.
Seriously, the following Haskell code:
Code 4
- Fact 0=1
- Fact n = n * Fact (N-1)
The above two lines of code define the function fact. Fact is the function name, followed by the fact, preceded by the equal sign, which is the function's argument. The function body is followed by the equals sign, and the result of the function body is the return value of the fact function.当程序员调用【fact 8】的时候(参数是8,因为Haskell函数调用一般不像C++那样给参数加括号),Haskell会将之匹配到上面代码的第2行。谁动了我的奶酪读书笔记(http://www.simayi.net/dushubiji/6208.html)摘抄好词好句及感悟赏析,这种参数匹配,是Haskell特有的函数声明与调用方式。
So the C + + template code in the previous code1 is mimicking the Haskell code in CODE4.
A complete Haskell program is given below
Modulefactwhere
Importsystem.io
Fact::integer->integer
Fact0=1
Fact n = n * Fact (N-1)
Main::io ()
Main=do
PUTSTRLN $ "8! = "+ + Show" (Fact 8)
PUTSTRLN $ "88! = "+ + Show" (Fact 88)
The result of the above code output is:
8! = 40320
88! =185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000 000000000000000
Haskell said to C + +: I can count 88!
C + + says: you bully people!
C + + Template programming implements Haskell's function pattern matching feature [figure]