刘未鹏|C++的罗浮宫

Knowledge sharing is the best reuse

刘未鹏ID:pongba
[修改头像]
707951次访问,排名42好友7人,关注者64
pongba的文章
原创 99 篇
翻译 8 篇
转载 0 篇
评论 1514 篇
刘未鹏的公告
除非特别声明,本站采用Creative Commons License许可。转载请注明作者、出处,非商业。

喜欢这个Blog的风格?见这里这里,还有这里

我的豆瓣饭否

讨论问题请到TopLanguage小组

TopLanguage

gtalk/msn(邮件请发送到gmail邮箱)

pongba@gmail.com
pp_liu@msn.com

订阅C++的罗浮宫

  • FeedSky
  • 订阅到鲜果
  • 订阅到Google
  • 订阅到抓虾
  • 订阅到BlogLines
  • XML聚合

搜索C++的罗浮宫上的内容

最新发表

    whaz going on


    饭否

    books I've translated




    最近评论
    pongba:@dd:
    误会了,不是不想,而是CSDN Blog不支持全文RSS。
    以前有一阵子倒是开放了全文RSS,我也跟着调整成了全文输出,但过了一阵子,又给关掉了。
    其实CSDN BLOG大可在RSS里面放广告嘛
    dd:RSS不是全文输出,惯用Google Reader和其它阅读器的读者可能会感觉不方便。
    为什么不改成全文输出呢?是技术问题,还是你很在意页面的点击量?
    顺便赞你的blog,很久以前,在你blog的重点还是C++语言的奇技淫巧时就开始看。:)
    刘未鹏:@william:
    是的。不过这里只是说明一个一般性原则。所以有些条件(如函数一阶可导)就不加赘述了。
    williamxu:Good job!
    "譬如通过驻点来求函数的最值,我们通过考察函数的最值(除了函数边界点外),发现它必然有一个性质,即在这个点上函数的一阶导数为0"
    是否应该是函数在区间上连续可导才成立,除边界点外,如果函数最值点不连续,是没有导数的。
    ppzhupapa:Read了one遍,确实is好article,but楼主write文章的style我feel不太good,mainly是many没necessary用English的places都using英语,no idea我这样say,楼主if or not看得understand.
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes
    文章分类
    收藏
    相册
    其它图片
    文章中的图片
    我的大头贴
    C++
    Andrei Alexandrescu
    Andrew Lumsdaine
    Bjarne Stroustrup
    boost
    C++ Standard Commitee
    Doug Gregor
    Hans J. Boehm
    Jaakko Jarvi
    Jeremy G. Siek
    Matthew Wilson
    newsgroups
    boost.Developer
    boost.User
    comp.lang.c++.moderated
    comp.std.c++
    TopLanguage
    Open Source
    codeplex
    Google AJAX Search API
    Google Code Prettify
    Google Web Toolkit
    MS shared source initiative
    notepad++
    STLSoft
    不认识的朋友们
    fatalerror99
    Yelz
    余晟|乱象&乱想
    刘慈欣
    姬十三
    张志强|阅微堂
    许式伟
    阮一峰
    鲍志云
    其它
    科学松鼠会
    科学美国人
    科幻世界
    认识的朋友们
    chenyufei
    duguguiyu|Venus神庙
    Joyfire
    littlestone
    lxwde
    Matrix67
    soloist
    云风
    刘江@图灵
    史晓明
    周星星
    周筠@博文视点
    孟岩
    张振
    徐宥|4G Spaces&Web 2.3
    方舟@博文视点
    李笑来|Pure Pleasure
    杨文博
    王信文|地球没有好朋友
    荣耀
    莫华枫
    袁泳(g9)|负暄琐话
    谢东升
    陈冀康@华章
    存档

    原创 为什么C++(来自C++之父的观点)

    新一篇: Joel Spolsky在耶鲁的演讲无责任导读

    为什么C++(来自C++之父的观点)

     

    By Bjarne Stroustrup

    http://www.research.att.com/~bs/

     

    刘未鹏(pongba)

    C++的罗浮宫(http://blog.csdn.net/pongba)

     

    注:9月份的时候写了一篇“Why C++”(翻译版见这里)。后来我把文章发给Bjarne,问问他的意见。Bjarne友好的对文章作了详细的评点并发回给我,我问他能不能把他的评点版发到blog上,他说原先发给我的评点比较随意,答应给我单独写一篇友情评注,也就是下面这篇。

     

    在后来的邮件交流中,我又问了Bjarne一些C++问题,并得知Bjarne的新书将会在夏季出版,在我们的交流中我曾问起他所说的正确的学习C++的方式到底是什么,他说他的新书将会是最好的答案!

     

    此外Bjarne还答应接受一次访谈,这个我已经在上次一篇blog里面提到了,引用如下:

     

    “最近和Bjarne Stroustrup的通信中,他答应接受一次访谈。
    为了让访谈能够代表更多人的意见而不是我个人的感觉,在这里邀请大家提出自己最想问的问题。
    你最想问什么?
    BTW.
    由于我会对问题进行筛选,然后再公布给大家投票。所以大家的问题最好是深思熟虑的,重要的,有理有据的:-)
    问题后如果能附上问题的背景(即你在什么样的经历中发现这个问题很关键)那是最好不过!
    问题可以留言在blog上,但更建议发到TopLanguage上,我专门开了一则帖子,这样大家可以互相讨论:-)

     

    以下是Bjarne的原文(抱歉,我没有时间全文翻译出来,如果哪位同学有兴趣译出来的话可以先跟我说一声——这是为了避免多人在互相不知道的情况下重复劳动。另外我会在翻译版上署上你的名字和链接(如果有的话)):

     

    Dear “pongba”,

     

    You sent me a long and thoughtful note about C++ and its use – or lack of use or misuse – in your environment. I answered with a set of disjointed comments on your note that unfortunately were not suitable for a wider audience, such as your blog. This is the slightly revised set of those notes – made a little bit more coherent, but still not of the quality of a proper paper – that you can post (incl. this little explanatory note):

     

    C and C++

    You worry that many people prefer to use C over C++ and that there may even be a move away from C++ towards C. You are being unnecessarily defensive. Don’t forget that for major applications, the move has been from C to C++ for a very long time. If people “frown and turn away,” they are displaying ignorance. Don’t be defensive: Challenge the unsupported myths! My applications page (http://www.research.att.com/~bs/applications.html) is a good place to start. If there really is a drift from C++ to C – which I have never seen in the US or Europe – then it speaks badly on the education of the “drifters”. Organizations caring about quality code have consistently drifted the other way.

     

    People often say that C is simpler than C++. They consider that obvious (just look at the thickness of the C standard compared with the C++ standard or K&R2 compared to TC++PL3. The question is “simpler for what?” I don’t know of any program that can be simpler when written in C than in C++; I don’t think such a program is possible. C++ provides better notational support and better type checking for the things that C does well. Improved type checking is important and its importance increases with the size of the program and the demands of correctness: C simply leaves you in the lurch and doesn’t provide facilities to express even a simple concept as “buffer” directly and without overheads. Don’t fall into the trap of equating C++ with OOP or GP. Those are powerful techniques where appropriate. Just because you don’t need OOP or GP for a project doesn’t imply that C is better for that. My claim is that it is not.

     

    C is not a simple language and programmers have no problems misusing it. Please explain the rules for conversion from unsigned short to int or try to teach a novice how to deal with pointers to pointers. Note the need for workarounds for just about any modern or large-program technique.  How many dark corners are there in C? Examples: explicit memory management, standards conversion and promotions, multidimensional array addressing, pointers to pointers, and macros. In the absence of C++ features, you get to use them all directly. How many necessary modern programming techniques do you have to apply through workarounds in C?

     

    Complexity will go somewhere: if not the language then the application code.

     

    It is sometimes claimed that C is more efficient than C++. There is absolutely no evidence that C is more efficient than C for any reasonable definition of “efficient”. There are fundamental reasons that C cannot be more efficient than C++: They share a machine model and a set of basic facilities to use it. For lots of details related to C++ performance see the ISO C++ committee’s Technical Report on performance (link on my C++ page: http://www.research.att.com/~bs/C++.html).

     

    But yes, the simplicity of C and its efficiency as compared to C++ are popular myths; in places they are so popular that people feel that no evidence is needed for their support. Unfortunately, such myths are not harmless; they damage the performance of individuals and organizations that buy into them. For a more detailed and precise statement of my opinion of C’s relationship to C++, see my papers on that topic: http://www.research.att.com/~bs/bs_faq.html#merge . See also my paper on learning C++: “Learning Standard C++ as a new language (link on my publications page).

     

    It is really hard to think of an area where C is better than C++ today. It would have to involve lack of decent C++ compilers and/or tools (such as CNU C++ or Microsoft C++ 10 years ago – many people still express strong opinions on C++ based on experience with such compilers) or it has to involve a lot of existing code that happens to make C++ hard to use (e.g. header files with lots of C++ keywords used as identifiers).

     

    C++ techniques

    C++ != OOP. Trust the programmer to choose which language facilities are appropriate – even your colleagues. Using C out of fear that someone will use an inappropriate language feature is bizarre: our systems are extremely complex and depriving the programmer of even type checking out of fear of complexity is either misguided or disingenuous.

     

    Yes, we could build a better string than std::string, but std::string is still better than *average* C string manipulation: gets(s) – enough said. Optimize only where needed; go for correctness and simplicity everywhere.

     

    There is no reason why code expressed using abstractions should be less efficient than equivalent hand written code (in C or C++) using lower-level features (pointers, char arrays, etc.). The most performance-critical C++ code tends to use templates (and classes): e.g., high-performance computation and serious embedded systems. See the ISO C++ Standard Committee’s Technical Report on Performance and the JSF++ coding standard (available from my C++ page).

     

    Obviously, a programmer can overuse abstraction mechanisms. Examples are designs that represent every idea as a class with lots of virtual functions in a single huge hierarchy and designs that represent every possible design decision as a template parameter. Good design is not just deferring all design decisions! Conversely, it is easy to underabstract. For example, try writing a set of mathematical functions for multi-dimensional arrays expressed directly as C/C++ multidimensional arrays. The result is something that passes array bounds as separate arguments – leading to bugs and run-time efficiencies.

     

    C++0x

    Some of the features coming in C++0x are exciting (e.g., concepts, uniform initialization, and the machine model), but we wont be able to rely on C++0x for another couple of years. C++0x shows where we are going, but if you want to defend C++, do it with minimal reference to C++0x. C++98 is a well-proven tool; C++0x will simply be better.

     

    There are technical embarrassments in C++, such as the need for a space between > and > in “vector<list<int>>”.  I was the one who named such technical problems “embarrassments” and listed several to encourage the standards committee to address them. No serious person considers any language in major real-world use perfect; they all have weaknesses that embarrass their expert users that should – if possible – be addressed.

     

    Have a look at my papers written for HOPL (The ACM History of Programming Languages conference) to get a more nuanced idea of how and why C++ evolved the way it did (links on my publications page: http://www.research.att.com/~bs/papers.html. The second paper covers the design criteria and ideas for C++0x.

     

    You said:

     

    The bottom line: keep simple things simple (but remember that simplicity can be achieved by using high-level libraries); use abstractions when necessary (and even then, make spare use of it; follow good design principles and established good practices).

     

    That’s sound advice, and the reference is good. Unfortunately, it is not easy to know when abstraction is beneficial and how much is “just right”. I would be a bit more aggressive about the use of classes and templates than you appear to recommend, but then I may be assuming a bit more experience. For a discussion of the use of abstraction see Bill Venner’s interview with me on that topic (http://www.artima.com/intv/goldilocks.html) that he entitled “The C++ Style Sweet Spot”.

     

    注:Bjarne在最后一段提到的访谈也是最技术的一篇,非常值得一读,这里推荐荣耀先生的翻译

    The C++ Style Sweet Spot (Part 1: C-style, object orientation, invariants, simple interfaces). Interview by Bill Venners for Artima.com. September 2003.

    Modern C++ Style (Part 2: Multiple inheritance, Abstract classes, multi-paradigm programming, and "resource acquisition is initialization"). Interview by Bill Venners for Artima.com. November 2003.

    Abstraction and Efficiency (Part 3: Raising the level of abstraction, "programming is understanding", and the difference between premature and prudent optimization). Interview by Bill Venners for Artima.com. February 2004.

    Elegance and other design ideals (Part 4: Aspects of software design, the importance of libraries, protecting programmers from themselves, the dangers of premature abstraction, and the essence of elegance). Interview by Bill Venners for Artima.com. February 2004.

     

    --

    1. 最近Feedburner抓取feed似乎出问题了,然而还有近200位同学通过Feedburner订阅了C++的罗浮宫(本来用FB的时候没预料到“墙”的问题,sigh~),所以,麻烦用FB订阅的同学转为用下面的feedsky订阅:-)

    http://feed.feedsky.com/pongba

    又或者你也可以直接订阅原始rss(因feedsky的服务似乎也似乎时有问题):

    http://blog.csdn.net/pongba/rss.aspx

    2. C++的罗浮宫已改为全文输出。

    3. As Always,欢迎加入我们的讨论组——TopLanguagehttp://groups.google.com/group/pongba),自从8月份建立以来,我们已经吸引了三百位同学,二千五百多条讨论,想知道我们讨论些什么,参见TopLanguage讨论精选[]精选[]精选[],加入前请看这里

     

    发表于 @ 2007年12月04日 15:50:00|评论(loading...)|编辑

    旧一篇: Beautiful Code, Beautiful Team ——《美丽的代码》译序(个人)

    评论

    #beride 发表于2007-12-04 17:00:38  IP: 192.168.119.*
    沙发?
    关注谁喜欢翻译~
    #Elan 发表于2007-12-04 17:15:02  IP: 203.93.229.*
    英文不好, 希望翻译, 麻烦
    #duyanning 发表于2007-12-04 21:34:21  IP: 202.117.10.*
    Complexity will go somewhere: if not the language then the application code.
    ======================
    对!若论简单,机器语言远比C更简单,只有0和1两个符号嘛。

    Good design is not just deferring all design decisions!
    =========================
    适可而止,太对了。


    问题,有,等我。
    #HolmesConan 发表于2007-12-04 23:05:13  IP: 121.19.223.*
    难道最近英语水平见长了?怎么感觉想在读chenglish
    o(∩_∩)o...
    dark corner?
    #Robbie Mosaic 发表于2007-12-05 12:48:35  IP: 211.167.159.*
    的确,如果不需要担心运行效率的话,还是应该以合适的抽象为目标,不要过于懒于抽象,否则程序就不好看也不好修改了。至于 C/C++,我觉得小程序我会用 C,中程序用 C++,但不一定广泛用 OO。大程序用 C#/Java 之类的语言。另外,C++ 的异常机制我想还是得进一步封装一下才好用,否则没有堆栈跟踪。如果想在 C 里面实现异常处理,可以考虑写点宏,使用 try...catch...finally 的形式而不是栈开解的形式。
    #koc105 发表于2007-12-05 15:25:54  IP: 202.69.89.*
    欢迎光临我的编程网 http://www.kingofcoder.com

    外国技术文章专区现在开张 - 目的是提供一个统一的地方, 给各编程爱好者阅读欣赏外国先进的编程技术
    http://english.kingofcoder.com
    #waterwalk 发表于2007-12-07 13:46:05  IP: 219.131.196.*
    "C is not a simple language and programmers have no problems misusing it. Please explain the rules for conversion from unsigned short to int or try to teach a novice how to deal with pointers to pointers. Note the need for workarounds for just about any modern or large-program technique. How many dark corners are there in C? Examples: explicit memory management, standards conversion and promotions, multidimensional array addressing, pointers to pointers, and macros. "

    关于这一段。c虽然也有不太好理解的地方,但c++不仅继承了这些不好理解的东西,还加入了好多新的不好理解的东西。c和qbasic比,是复杂了点,可是和c++比,语法上还是简单很多的。

    对于这些“不好理解”的东西,我觉得很大程度上是因为常规的教科书很少会详细解释其中的方方面面,所以经常是一头雾水。如果解释清楚了,其实也就明白了,最多只是规则繁一点。可惜有时“清楚地解释”不好找。
    #pongba 发表于2007-12-07 14:22:01  IP: 222.94.3.*
    Bjarne这里的意思是C++提供了抽象机制把程序员同从C继承来的一些复杂性隔离开来。
    例如,vector把程序员从原始的数组隔离开来。智能指针把程序员从手动管理内存释放的麻烦隔离开来。template消除了同构的重载。
    然而,没错,C++也引入了更多的复杂性。这个复杂性来自1)C继承来的缺陷和陷阱。2)更多的缺陷以及缺陷导致的惯用法。3)语言特性增加导致的语法细节增加。
    但后面这三点其实不是Bjarne这里要讲的意思,C++的复杂性并非在于引入了抽象机制,而在于引入了不完美的抽象机制。所以,不应该怪比如说vector或template或smart pointer太复杂,而应该怪C++在抽象机制的支持上还没有进化到最好。
    #waterwalk 发表于2007-12-18 09:01:26  IP: 219.131.196.*
    BS的新书是面向初学者的?是类似C++ Primer那种很全面的还是象Essential C++那种简单介绍性的?
    #pongba 发表于2007-12-19 14:30:12  IP: 222.94.3.*
    to waterwalk:
    是面向初学者的:-)
    比Essential C++更全面,但没有C++ Primer那样面向语言机制,侧重用本。
    #flynetcn 发表于2008-02-26 09:56:23  IP: 124.254.12.*
    还有两年就进入2010年了,C++0X啥时候能出来?会不会成为C++1X?
    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 刘未鹏