-
Notifications
You must be signed in to change notification settings - Fork 65
fixbug of lambda #48
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
fixbug of lambda #48
Conversation
muyuuuu
commented
Sep 25, 2024
•
edited
Loading
edited
- 修复一些 bug
- 这篇文章之前没有讲模板相关的,所以增加了模版那块的调用,让土木老哥也能看懂
docs/lambda.md
Outdated
@@ -526,6 +526,20 @@ int generic_sum(std::vector<int> const &v, Op op) { | |||
|
|||
> {{ icon.fun }} C++11:auto 只能用于定义变量;C++14:函数返回类型可以是 auto;C++17:模板参数也可以 auto;C++20:函数参数也可以是 auto 了;(狂想)C++47:auto 现在是 C++47 的唯一关键字,用户只需不断输入 auto-auto-auto,编译器内建人工智能自动识别你的意图生成机器码。 | |||
|
|||
使用模板调用时,通常需要指定模板参数 `Op` 的类型: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我不认为显式写明函数模板类型是“通常”,通常应当是“编译器推导”。
写明在绝大多数情况下是冗余和没必要的,比如此处。
同时,写明 std::function<int(int,int)>
也是没价值的,模板本身就能自行推导类型,接受所有可调用类型(此处无法接受成员指针),使用 std::function
这种多态包装器,不单是看着冗余,同时也会带来更大的开销。
同时原文也还没有出现 std::function
,无需额外引入。
docs/lambda.md
Outdated
generic_sum(a, add); | ||
``` | ||
|
||
这和上面的代码是等价的。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
而且这里编译器自动推导出来的Op
不是std::function<int(int, int)>
,而是Lambda的匿名类型,decltype(add)
。这两个类型并不相同,function
是一个类型擦除容器是标准库的一部分,而Lambda匿名类型是语言的一部分,并且不涉及std名字空间,只是一个空的结构体。
这里说“等价的”疑似有点误导小朋友了,要说也应该说等价于:
generic_sum<decltype(add)>(a, add);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有些人不知道这个简单的事实:
std::function<void(int)> fun = [] (int i) {}
发生了隐式类型转换。
说Lambda的类型就是std::function
,就好比说const char *
就是std::string
一样,根本是误导小朋友。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
而是Lambda的匿名类型
lambda 表达式的类型就是非聚合非联合的类类型。它是个类,简单来说就是重载了 operator()
的类:
struct Lambda{
auto operator(...){
...
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有些人不知道这个简单的事实:
std::function<void(int)> fun = [] (int i) {}发生了隐式类型转换。 说Lambda的类型就是
std::function
,就好比说const char *
就是std::string
一样,根本是误导小朋友。
这里如果要清楚的说,会很麻烦,简单来说:
std::function
有转换构造函数,lambda 表达式(即 lambda 类对象)得以调用这个转换构造函数,构造出这一个临时的 std::function
对象,然后再去初始化 fun。
这里初始化 fun 有几种可能,如果达到 C++17 那么确保复制消除,无额外开销(因为是同类型的临时量),如果没有达到 C++17,则可能走移动构造,或者被 NRVO 优化。
这里的一个 =
,复制初始化的事情太多,没法。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是我想简单了,又去学了下 std::function 的类型擦除,大概理解了这部分
docs/lambda.md
Outdated
generic_sum<std::function<int(int, int)>>(a, add); | ||
``` | ||
|
||
`std::function<int(int, int)>` 是什么鬼画符?别担心小彭老师会在下文讲解 `function` 这个容器。这里可以简单理解为这是一个接受两个 `int` 类型的参数,返回值也是 `int` 类型的函数容器,容纳 `add` 这个函数。不过也可以让编译器去自动推导模板参数: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
鬼画符是什么意思?建议把这部分改了,或者删了,总之,如果你要添加新章节的话可以单独开一个pr,修bug单独一个pr,不要尝试在一个pr里塞太多不直接相关的东西。如果你这个pr只包含bugfix,那么我现在已经可以直接合并,但是因为你自作主张地把一个新文案加进来,而且文案写的还有问题,导致我们需要把你这部分的文案给审了,才能把本来就没问题的bugfix合并。总之,pr越多越好,越小越好,越专一越好。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
鬼画符
名词解释“鬼画符”:
- 常见于老师对于学生作业的评价,大多指混乱,字体难看,难以理解。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要抱着那种”pr开太多“”一个pr只改动一行“会不会“打扰”到小彭老师的心态,邮箱消息本来就不像“微信qq”那样会突然一个“弹窗”骚扰你,本来就是登github的时候一次性来看一眼的,你如果开了100个pr,每个只修一个bug,我反而会非常高兴,因为你懂得把无关的改动分开,避免因为一个改动不过关来回审核半天。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想必你也不喜欢捆绑软件,我审核这种捆绑了一大堆不相干改动的pr,就像下载捆绑软件一样,我只想要下个photoshop,你给我捆绑一个360壁纸看看助手、丑图秀秀算什么意思,我如果需要丑图秀秀,我自己会安装,不需要你捆绑。
6f0cc2e
to
9718c84
Compare