有效的 C++ 仍然有效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17866735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is effective C++ still effective?
提问by German Capuano
From what I saw in this postI decided to start reading the book Effective C++.
根据我在这篇文章中看到的内容,我决定开始阅读Effective C++一书。
But now that there are many new features because of C++11 and that a few of the good practices changed, I'm not sure whether or not it is actually a good idea. Has the advent of C++11 deprecated any of the advice contained in Effective C++? If so, which topics should I avoid?
但是既然 C++11 带来了许多新特性,并且一些好的做法发生了变化,我不确定这是否真的是一个好主意。C++11 的出现是否弃用了 Effective C++ 中包含的任何建议?如果是这样,我应该避免哪些主题?
采纳答案by TemplateRex
This what Scott Meyers himself had to say about it on his own blog
这就是 Scott Meyers 本人在他自己的博客上所说的
Which may lead you to wonder whether the information and advice in this pre-C++0x edition of Effective C++ remains relevant. I'm pleased to report that it does. Surprisingly so, in fact. Having spent nearly two years steeped in the details of C++0x, I expected to groan a bit as I reviewed this book's table of contents with C++0x in mind. Surely some Items would be inappropriate. But the advice I found proved sound. Should C++0x developers prefer consts, enums, and inlines to
#defines
(Item 2)? They should. Should they prevent exceptions from leaving destructors (Item 8)? Certainly. Should they use objects to manage resources? Declare data members private? Consider alternatives to virtual functions? Factor parameter-independent code out of templates? (Items 13, 22, 35, and 44.) Yes, yes, yes, yes! My goal has always been for Effective C++'s table of contents to summarize the advice in the book, and that summary remains just as applicable to C++0x development as to “traditional” C++ development. C++0x is a bigger language, and in some ways it's a different one, but the core techniques for making effective use of “old” C++ are core for the effective use of C++0x, too.This doesn't mean that this Effective C++ is a perfect match for C++0x. The book doesn't discuss features new to C++0x, so Items about making effective use of those features are missing. A C++0xified Effective C++ would certainly have Items devoted to move operations, to uniform initialization, and to lambda expressions, and it'd probably have an entire chapter on making effective use of the concurrency API. Such a book would also contain different examples, e.g., ones making use of auto variables, range-based for loops, in-class default initializers, as well as the occasional variadic template. To the extent that this book falls short in its support for C++0x, the errors are those of omission, not commission.
这可能会让您怀疑,Effective C++ 的这个 C++0x 之前版本中的信息和建议是否仍然相关。我很高兴地报告它确实如此。令人惊讶的是,事实上。花了将近两年时间沉浸在 C++0x 的细节中,当我在考虑 C++0x 的情况下查看本书的目录时,我预计会有点呻吟。当然有些项目是不合适的。但我发现的建议被证明是合理的。C++0x 开发人员是否应该更喜欢常量、枚举和内联?
#defines
(第 2 项)?他们应该。它们是否应该防止异常离开析构函数(第 8 条)?当然。他们应该使用对象来管理资源吗?将数据成员声明为私有?考虑替代虚函数?从模板中分解出与参数无关的代码?(第 13、22、35 和 44 项。)是的,是的,是的,是的!我的目标一直是使 Effective C++ 的目录总结本书中的建议,并且该总结仍然适用于 C++0x 开发和“传统”C++ 开发。C++0x 是一种更大的语言,在某些方面它是一种不同的语言,但有效使用“旧”C++ 的核心技术也是有效使用 C++0x 的核心。这并不意味着这个 Effective C++ 是 C++0x 的完美匹配。本书没有讨论 C++0x 的新特性,因此缺少关于有效使用这些特性的条目。C++0xified Effective C++ 肯定会有专门用于移动操作、统一初始化和 lambda 表达式的项目,并且它可能有一整章关于有效使用并发 API。这样的书还将包含不同的示例,例如,使用自动变量、基于范围的 for 循环、类内默认初始化器以及偶尔使用的可变参数模板的示例。就本书对 C++0x 支持的不足而言,错误是疏忽,而不是委托。
UPDATE: the new title Effective Modern C++has been for sale since November 2014 from O'Reillyand Amazon(and many others that you can google for).
更新:新书Effective Modern C++自 2014 年 11 月起从O'Reilly和亚马逊(以及许多其他你可以谷歌搜索)出售。
回答by cdmh
Yes, definitely still worth reading. There is a new book that Scott is working on: Effective C++11, which concentrates on C++11 only code. The Effective C++is still very relevant, and is not superseded by the new book. Buy it, read it, enjoy :)
是的,绝对值得一读。Scott 正在撰写一本新书:Effective C++11,它只专注于 C++11 代码。在有效的C ++仍然是非常相关的,而不是由新书取代。购买,阅读,享受:)
回答by djechlin
One, the book is still of course valid for C++03.
一,本书当然对 C++03 仍然有效。
Two, Meyers is writing or has written, depending on when you are reading this sentence, Effective C++11.
二、迈耶斯正在写或已经写,这取决于你什么时候读这句话,Effective C++11。
Three, speaking in generality, the points of this book will still be valid. C++ still favors speed over safety, and many issues in Effective C++revolve around this.
三、笼统地讲,本书的观点仍然有效。C++ 仍然偏爱速度而不是安全,Effective C++ 中的许多问题都围绕着这一点。
The onlykind of point I would expect to be invalid are ones that say "Don't do this, do this instead." "Dont' do this" will still be valid. But C++ will have solved some problems. In particular, any recommendations to use boost are likely deprecated, as C++11 has included many of the features Meyers specifically refers the reader to Boost for.
该只种一点我希望是无效的是那些说“不要这样做,这样做吧。” “不要这样做”仍然有效。但是C++已经解决了一些问题。特别是,任何使用 boost 的建议都可能被弃用,因为 C++11 已经包含了许多 Meyers 特别推荐读者使用 Boost 的特性。