C++ 泛型编程/模板 泛函编程/Lambda/λ演算

1、泛型编程(C++模板)

其中,Ada, Delpha, Java, C#, Swift 称之为 泛型/generics; ML, Scala和 Haskell 称之为 参数多态/parametric polymorphism; C++和D语言称之为 模板/template. 《设计模式/Design Patterns》称之为 参数化类型/parameterized type. 因为在这里,参数的类型在一般情况下都是未知的,而泛型编程可以支持多种类型,所以叫泛/generic

①函数模板/Function Template:定义一个函数模板,可以支持多种类型的参数

The format for declaring function templates with type parameters is:

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

比如, the C++ Standard Library contains the function template max(x, y) which returns the larger of x and y. That function template could be defined like this:

template <typename T>
inline T max(T a, T b) {
    return a > b ? a : b;
}

当我们调用这个 Template Function的时候,只要max(x1, x2)就可以了,编译器会自动选择对应的typename。

函数模板/模板函数/Function template 可以 重载/overload,也就是说还可以定义与 函数模板同名的函数。

例如:

#include <iostream>
#include <algorithm>

template <typename T>
inline T max(T a, T b) {
    return a > b ? a : b;
}

// 模板函数 重定义/overload/重载
int max(int a, int b)
{
    return a>b ? a : b;
}


int main()
{
    std::cout << max(3,7) << std::endl;
    std::cout << max(3.0, 7.0) << std::endl;
    std::cout << max(3, 7.0) << std::endl;// 调用 重载/overloaded 的函数

    std::cout << max<double>(3, 7.0) << std::endl;

    return 0;
}

REFER: Function templates

②类模板/Class Template

REFER: Class Template

2、泛函编程/函数式编程/Functional Programming

其,将电脑运算比作 数学 上的函数计算,并且避免使用程序状态以及易变物件。在 Functional Pgramming 中,最重要的基础是 λ演算/lambda calculus。

纯粹的函数式编程语言: Haskell,Miranda,Concurrent Clean。
非纯函数式编程语言:F#, Scala, Erlang, LISP, Mathematicar。

转载于:https://www.cnblogs.com/xuanyuanchen/p/6039258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值