V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dorafmon
V2EX  ›  推广

给新手 C++ Developer 的 Tips 1

  •  
  •   dorafmon · 2021-01-12 18:24:14 +08:00 · 1799 次点击
    这是一个创建于 1192 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,我又来安利我的视频了,因为在平时工作 /面试的过程中经常会遇到一些关于 C++的细节,而这些知识点大多散落在各种书 /博客 /github repo 里,所以我想把这些知识总结成每个 10mins 左右的视频,分享给大家,也是我自己对自己知识图谱的一个梳理。我在录这个视频的时候面对的对象人群是 junior C++ developer,想好好准备 C++知识面试,或者是想学习一些跟 C++有关的能应用在工作中的 best practice 。因为我自己在国外,所以有些时候有些中英混杂,实在不好意思。

    下面是第一个视频,讲的是关于 C++ exception 的一个小知识点~

    C++“博大精深”,我也只是个小学生,难免会出错,还请大家斧正。

    也烦请大家 subscribe 我的 youtube 频道: https://www.youtube.com/channel/UCg4d15bORamNqf-fPz9e9aw

    视频地址: B 站: https://www.bilibili.com/video/BV1jf4y1y7gc youtube: https://www.youtube.com/watch?v=T5Z2iFHpoy8

    8 条回复    2021-01-14 17:20:31 +08:00
    wutiantong
        1
    wutiantong  
       2021-01-12 22:10:52 +08:00
    没有稿子磕磕绊绊的讲解令人难以忍受。
    另外我查了,析构函数是可以抛异常的,即使在 stack unwinding 中也不是未定义行为:

    As any other function, a destructor may terminate by throwing an exception (this usually requires it to be explicitly declared noexcept(false)) (since C++11), however if this destructor happens to be called during stack unwinding, std::terminate is called instead.
    https://en.cppreference.com/w/cpp/language/destructor
    dorafmon
        2
    dorafmon  
    OP
       2021-01-12 22:26:43 +08:00
    @wutiantong 嗯嗯 下回我争取先写个稿子。 这个我讲了一般是会 terminate 或者是 undefined behavior,effective C++原文也是这么写的。我下回更严谨一点,fact check 一下。
    dorafmon
        3
    dorafmon  
    OP
       2021-01-12 22:32:05 +08:00
    @wutiantong 书里的原文: “Depending on the precise conditions under
    which such pairs of simultaneously active exceptions arise, program execution either terminates or yields undefined
    behavior”, 考虑到书挺老的确实我应该更严谨一点,但是这个 point 没怎么变,还是不鼓励大家 throw 。
    wutiantong
        4
    wutiantong  
       2021-01-13 11:08:23 +08:00
    @dorafmon

    是有这样的说法,但既然不是 ub 就可以在充分了解的前提下加以利用。

    我倒想考考,你能简洁地讲清楚 noexcept 应该怎么写么?
    dorafmon
        5
    dorafmon  
    OP
       2021-01-13 16:44:38 +08:00
    @wutiantong 我做这个视频的只是想给普通的新入行 c++程序员提供一点社区内都知道的常识。所以有些时候有些特殊情况我就没有提,有些特别细节的东西我自己也会有所取舍,我觉得不鼓励大家在 destructor throw exception 其实算是业界的一个普遍看法。

    noexcept 这个问题我可能要做点研究才能完整的回答你,但是我目前的看法是不要随便乱加 noexcept,因为这个会成为你 api specification 的一部分,你的 function 以后如果要 throw 的话就意味着你要改变这个 spec,所以在你不是非常确定的情况下暂时不要加 noexcept 。
    wutiantong
        6
    wutiantong  
       2021-01-14 14:21:05 +08:00
    @dorafmon

    这个知识点是没问题的,但你的论述可能是有问题的。

    我们在 C++ Core Guidelines 中可以找到关于这个知识点更清晰更正确的论述:

    https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-dtor-fail
    wutiantong
        7
    wutiantong  
       2021-01-14 14:23:13 +08:00
    @dorafmon 你没有认识到 noexcept 这个关键词的设计意图
    dorafmon
        8
    dorafmon  
    OP
       2021-01-14 17:20:31 +08:00
    @wutiantong 嗯嗯,受教了,我再打磨打磨自己。关于这个 noexcept,您能更详细的讲讲么?谢谢!我自己也想学习学习。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5737 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:09 · PVG 10:09 · LAX 19:09 · JFK 22:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.