简体   繁体   English

是c ++模板元编程是函数式编程的一种形式

[英]is c++ Template Metaprogramming a form of functional programming

is c++ Template Metaprogramming a form of functional programming? 是c ++模板元编程是一种函数式编程吗? If it is, do some pitfalls like stackoverflow for non-tail recursion relevant for c++ Template Metaprogramming? 如果是的话,为c ++模板元编程相关的非尾递归做一些像stackoverflow这样的陷阱吗?

For the factorial template example in this question , I guess it is standard functional programming. 对于这个问题中的阶乘模板示例,我想这是标准的函数式编程。 Or the similarity is only superficial? 或者相似性只是肤浅的?

#include <iostream>
using namespace std;

template< int n >
struct factorial { enum { ret = factorial< n - 1 >::ret * n }; };

template<>
struct factorial< 0 > { enum { ret = 1 }; };

int main() {
    cout << "7! = " << factorial< 7 >::ret << endl; // 5040
    return 0;
}

is c++ Template Metaprogramming a form of functional programming? 是c ++模板元编程是一种函数式编程吗?

Yep! 是的! Template expansion has no side effects, so it is pure-functional. 模板扩展没有副作用,因此它是纯功能的。

If it is, do some pitfalls like stackoverflow for non-tail recursion relevant for c++ Template Metaprogramming? 如果是的话,为c ++模板元编程相关的非尾递归做一些像stackoverflow这样的陷阱吗?

Absolutely. 绝对。 Factorial isn't a good demonstration of this, since the result will overflow long before your stack does, but long recursions can definitely cause the compiler to error out. Factorial不是一个很好的证明,因为结果会在堆栈运行之前很久就会溢出,但长递归肯定会导致编译器出错。 Interestingly, however, compilers tend to implement templates in such a way that you get automatic memoization. 然而,有趣的是,编译器倾向于以这样的方式实现模板,以便您获得自动memoization。 For instance, a naively written implementation of the Fibonacci series will tend to compile in O(n) time rather than O(2^n). 例如,Fibonacci系列的天真编写实现将倾向于在O(n)时间而不是O(2 ^ n)编译。

I think the best answer is that the two concepts are completely different things. 我认为最好的答案是这两个概念完全不同。 However, that does not really help, so here are some key points that I can think of: 但是,这并没有什么帮助,所以这里有一些我能想到的关键点:

  • Both C++ templates and (parametric polymorphism in) functional languages like ML are examples of generic programming and so it makes some sense to mention them together in a single question. 像ML这样的函数式语言中的C ++模板和(参数多态)都是泛型编程的例子,因此在一个问题中一起提及它们是有意义的。 In academia, there is even Workshop on Generic Programming that often covers C++ template stuff and ML-style functional programming. 在学术界,甚至还有通用编程研讨会,它通常涵盖C ++模板和ML风格的函数式编程。

  • However, template metaprogramming gives you a way to write code that gets "evaluated" during the compilation of your C++ code and so you can use it only for quite limited tasks. 但是,模板元编程为您提供了一种编写在编译C ++代码期间“评估”的代码的方法,因此您只能将它用于非常有限的任务。 On the other hand, functional programs normally run, well, at runtime and you can define complex data structures, build abstractions etc. (you can do some things using template metaprogramming, but it will look ugly). 另一方面,功能程序通常在运行时运行,您可以定义复杂的数据结构,构建抽象等(您可以使用模板元编程来做一些事情,但它看起来很难看)。

  • Why is the factorial example similar to factorials in functional programming? 为什么阶乘示例类似于函数式编程中的阶乘? When you're using template meta-programming, you cannot define "mutable variables" (because you are just defining new types or templates) and so template meta-programming might feel a bit like functional programming style. 当您使用模板元编程时,您无法定义“可变变量”(因为您只是定义新类型或模板),因此模板元编程可能会感觉有点像函数式编程风格。

  • Code written using template metaprogramming is evaluated at compile-time. 使用模板元编程编写的代码在编译时进行评估。 This means that it cannot depend on user input! 这意味着它不能依赖于用户输入! It either compiles or not (the compiler may have some restriction on the depth of recursion and give up after some time). 它是编译与否(编译器可能对递归的深度有一些限制,并在一段时间后放弃)。 On the other hand, functional programs can take some input and then evaluate (which means they can use all the stack or memory available and then fail). 另一方面,功能程序可以接受一些输入然后进行评估(这意味着它们可以使用所有可用的堆栈或内存然后失败)。

For functional programmers, I know that some functional languages like Agda can also evaluate things at compile-time, but I think it is better to keep things simple! 对于函数式程序员,我知道像Agda这样的函数式语言也可以在编译时对事物进行评估,但我认为最好保持简单!

Metaprogramming do all the business in compile time which has many pointer same with functional style. 元编程在编译时完成所有业务,其中许多指针与功能样式相同。 But they are not the same thing. 但它们不是一回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM