c++ - 当 C++14 已经有泛型 lambda 时,在 C++20 中引入模板 lambda 的需要是什么?
问题描述
c++14引入了通用 lambda,使得编写以下内容成为可能:
auto func = [](auto a, auto b){
return a + b;
};
auto Foo = func(2, 5);
auto Bar = func("hello", "world");
很明显,这个通用 lambdafunc
就像模板函数func
一样工作。
为什么 C++ 委员会决定为泛型 lamda 添加模板语法?
解决方案
C++14 泛型 lambda 是一种非常酷的方式来生成具有operator ()
如下所示的仿函数:
template <class T, class U>
auto operator()(T t, U u) const;
但不是这样:
template <class T>
auto operator()(T t1, T t2) const; // Same type please
也不是这样:
template <class T, std::size_t N>
auto operator()(std::array<T, N> const &) const; // Only `std::array` please
也不是这样(尽管实际使用起来有点棘手):
template <class T>
auto operator()() const; // No deduction
C++14 lambda 很好,但 C++20 允许我们轻松实现这些情况。
推荐阅读
- java - 两个重载方法必须具有相同的返回值,才能称为“重载”?
- python - 从数据框中使用 Dash Cytoscape
- javascript - Jquery不处理href点击
- javascript - AJAX Https 请求显示 CORS 错误。(来自本地服务器)
- javascript - 仅当我尝试通过 mask-image 加载图像时收到 CORS 错误:url()
- macos - 无法连接到任何机器:端口 22:操作超时(BigSur)
- android - 如何通过意图将pdf文件上传到电子邮件应用程序
- c# - 是否可以使用本地以及 azure B2c 对 mvc 应用程序进行身份验证
- python - 在 tkinter 中实现分屏时出错
- firebase - Firebase 和 Flutter 如何构建我的数据以及如何通过检查用户名是否已存在来避免高成本