现代 C++ 越来越流行了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/536439/
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 modern C++ becoming more prevalent?
提问by jalf
When I first learned C++ 6-7 years ago, what I learned was basically "C with Classes". std::vector
was definitely an advanced topic, something you could learn about if you reallywanted to. And there was certainly no one telling me that destructors could be harnessed to help manage memory.
Today, everywhere I look I see RAII and SFINAEand STL and Boost and, well, Modern C++. Even people who are just getting started with the language seem to be taught these concepts almost from day 1.
6-7 年前我第一次学习 C++ 时,我学到的基本上是“C with Classes”。std::vector
绝对是一个高级主题,如果你真的想学习,你可以学习。当然没有人告诉我可以利用析构函数来帮助管理内存。今天,我到处都能看到 RAII、SFINAE、STL 和 Boost 以及现代 C++。即使是刚开始使用这门语言的人,似乎也几乎从第一天就开始学习这些概念。
My question is, is this simply because I'm only seeing the "best", that is, the questions here on SO, and on other programming sites that tend to attract beginners (gamedev.net), or is this actually representative of the C++ community as a whole?
我的问题是,这仅仅是因为我只看到“最好的”,即 SO 上的问题,以及其他倾向于吸引初学者的编程网站 (gamedev.net),还是这实际上代表了整个 C++ 社区?
Is modern C++ really becoming the default? Rather than being some fancy thing the experts write about, is it becoming "the way C++ just is"?
Or am I just unable to see the thousands of people who still learn "C with classes" and write their own dynamic arrays instead of using std::vector
, and do memory management by manually calling new/delete from their top-level code?
现代 C++ 真的成为默认了吗?它不是专家们写的一些花哨的东西,而是成为“C++的方式”?或者我只是无法看到成千上万的人仍然在学习“C with classes”并编写自己的动态数组而不是使用std::vector
,并通过从他们的顶级代码中手动调用 new/delete 来进行内存管理?
As much as I want to believe it, it seems incredible if the C++ community as a whole has evolved so much in basically a few years. What are your experiences and impressions?
尽管我很想相信,但如果整个 C++ 社区在基本上几年内发生了如此大的发展,这似乎令人难以置信。你有什么经历和印象?
(disclaimer: Someone not familiar with C++ might misinterpret the title as asking whether C++ is gaining popularity versus other languages. That's not my question. "Modern C++" is a common name for a dialect or programming style within C++, named after the book "Modern C++ Design: Generic Programming and Design Patterns Applied", and I'm solely interested in this versus "old C++". So no need to tell me that C++'s time is past, and we should all use Python ;))
(免责声明:不熟悉 C++ 的人可能会将标题误解为问 C++ 是否比其他语言更受欢迎。这不是我的问题。“现代 C++”是 C++ 中方言或编程风格的通用名称,以这本书命名“ Modern C++ Design: Generic Programming and Design Patterns Applied”,我只对这个和“旧 C++”感兴趣。所以不需要告诉我 C++ 的时代已经过去,我们都应该使用 Python ;))
采纳答案by JohnMcG
Here's how I think things have evolved.
这就是我认为事情已经发展的方式。
The first generation of C++ programmers were C programmers, who were in fact using C++ as C with classes. Plus, the STL wasn't in place yet, so that's what C++ essentially was.
第一代 C++ 程序员是 C 程序员,他们实际上将 C++ 用作带有类的 C。另外,STL 还没有到位,所以这就是 C++ 的本质。
When the STL came out, that advanced things, but most of the people writing books, putting together curricula, and teaching classes had learned C first, then that extra C++ stuff, so the second generation learned from that perspective. As another answer noted, if you're comfortable writing regular for loops, changing to use std::for_each
doesn't buy you much except the warm fuzzy feeling that you're doing things the "modern" way.
当STL出来的时候,那是先进的东西,但是大多数写书、整理课程和教课的人都是先学C,然后是额外的C++东西,所以第二代从这个角度学习。正如另一个答案指出的那样,如果您习惯于编写常规 for 循环,那么更改为 usestd::for_each
不会给您带来太多好处,除了您正在以“现代”方式做事的温暖模糊感觉。
Now, we have instructors and book writers who have been using the whole of C++, and getting their instructions from that perspective, such as Koenig & Moo's Accelerated C++ and Stroustrup's new textbook. So we don't learn char*
then std::strings
.
现在,我们有一直在使用整个 C++ 的讲师和书籍作者,并从这个角度得到他们的指导,例如 Koenig & Moo 的 Accelerated C++ 和 Stroustrup 的新教科书。因此,我们不学char*
则std::strings
。
It's an interesting lesson in how long it takes for "legacy" methods to be replaced, especially when they have a track record of effectiveness.
关于替换“遗留”方法需要多长时间,这是一个有趣的教训,尤其是当它们具有有效性的跟踪记录时。
回答by Ray Hidayat
Absolutely yes. To me if you're not programming C++ in this "Modern C++" style as you term, then there's no point using C++! You might as well just use C. "Modern C++" should be the only way C++ is ever programmed in my opinion, and I would expect that everyone who uses C++ and has programmed in this "Modern" fashion would agree with me. In fact, I am always completely shocked when I hear of a C++ programmer who is unaware of things such as an auto_ptr or a ptr_vector. As far as I'm concerned, those ideas are basic and fundamental to C++, and so I couldn't imagine it any other way.
绝对没错。对我来说,如果你不是按照你所说的“现代 C++”风格来编程 C++,那么使用 C++ 是没有意义的!你也可以只使用 C。在我看来,“现代 C++”应该是 C++ 编程的唯一方式,我希望每个使用 C++ 并以这种“现代”方式编程的人都会同意我的看法。事实上,当我听到一个 C++ 程序员不知道诸如 auto_ptr 或 ptr_vector 之类的东西时,我总是完全震惊。就我而言,这些想法是 C++ 的基础和基础,所以我无法想象其他任何方式。
回答by spoulson
In the days of Windows 3.1, C was the standard. When C++ hit the developer market and later became ANSI standard, it was the new hotness. It popularized the OOP acronym and some of the basic design patterns using polymorphism.
在 Windows 3.1 时代,C 是标准。当 C++ 进入开发者市场并后来成为 ANSI 标准时,它成为了新的热点。它普及了 OOP 首字母缩写词和一些使用多态的基本设计模式。
Now, with the greater acceptance of low-barrier-to-entry managed platforms, like C#/.NET, there's less of a reason to use C++. So much of the developer base will have a choice and let's be honest: C++ is a bear to learn for a novice. With C#, you can just run with it.
现在,随着对入门门槛低的托管平台(如 C#/.NET)的接受度越来越高,使用 C++ 的理由越来越少。如此多的开发人员基础将有一个选择,让我们说实话:C++ 是一个初学者学习的熊。使用 C#,您可以直接运行它。
That leaves really only the platforms that NEED C++ and the die-hard C++ evangelists to continue practicing the art. This is the community that needs and wants all the layers of abstraction that is considered "Modern C++".
这实际上只剩下需要 C++ 的平台和顽固的 C++ 传道者继续实践艺术。这是需要并想要被认为是“现代 C++”的所有抽象层的社区。
So yes, I believe "Modern C++", as you state it, is becoming more prevalent. Albeit, it's prevalent with a different audience than has used it in the past.
所以是的,我相信“现代 C++”,正如你所说的,正变得越来越流行。尽管如此,它在与过去使用它的受众不同的情况下很普遍。
回答by jfsantos
I am one of these guys who learned how to work with the STL and heard a lot about RAII and good C++ programming practices from day 1. Looks like some of the most recommended books for learning C++ today (like Accelerated C++ and the Effective C++ series) focus on using STL tools instead of rolling up your own stuff, and also give lots of "rules" for effective (or "modern") programming.
我是学习如何使用 STL 的人之一,从第一天起就听说了很多关于 RAII 和良好的 C++ 编程实践的人。 ) 专注于使用 STL 工具而不是卷起自己的东西,并且还为有效(或“现代”)编程提供了许多“规则”。
But talking with friends I also noted some companies still work with "C with Classes", not "Modern C++". Maybe the culture proposed by the authors and users of the "Modern C++" will prevail someday :)
但与朋友交谈时,我也注意到一些公司仍在使用“C with Classes”,而不是“Modern C++”。也许有一天“现代 C++”的作者和用户提出的文化会盛行:)
回答by Binary Worrier
I think you just had a bad experience starting off.
我想你刚开始的经历很糟糕。
You need to get yourself Scott MeyersEffective C++ books. I started on C++ in anger in 1999, my team lead made me sit and read Effective C++ and More Effective C++ before I was allowed to check in ANY code.
你需要给自己买Scott MeyersEffective C++ 书籍。我在 1999 年愤怒地开始使用 C++,我的团队领导让我坐下来阅读 Effective C++ 和 More Effective C++,然后才被允许检查任何代码。
Most of his advice is on the lines of "Don't use this feature, but if you must, keep thisin mind"
他的大部分建议都是“不要使用此功能,但如果必须,请记住这一点”
If you follow his advice you'll write good or "Modern" C++.
如果你听从他的建议,你就会写出好的或“现代”C++。
He has a book on STL now too, but that I haven't read.
他现在也有一本关于 STL 的书,但我还没有读过。
回答by David Thornley
In my C++ jobs, I've found the modern features to be increasingly used, and more people asked me about them in phone screenings and interviews. As far as I can tell, they're catching on.
在我的 C++ 工作中,我发现越来越多地使用现代功能,越来越多的人在电话筛选和面试中向我询问它们。据我所知,他们正在迎头赶上。
I learned C++ originally as something like C with Classes; although the language had advanced far beyond that, the books I read and people I worked with were firmly stuck on "old C++". RAII something people would think about, rather than automatically do, and I remember reading some of the early articles on the problems of exception safety.
我最初学习 C++ 是作为类似于 C 与类的东西;尽管这门语言的进步远远不止于此,但我读过的书和与我共事的人都牢牢地停留在“旧 C++”上。RAII 是人们会想到的,而不是自动做的,我记得阅读过一些关于异常安全问题的早期文章。
As pointed out, there's new books out now. Many of the old ones are still relevant, but they increasingly seem to be full of explaining why obviously bad ideas are bad. (Similarly, it's hard for modern readers to understand how revolutionary Freud's ideas of an unconscious mind were, since it's now conventional wisdom.)
正如所指出的,现在有新书出版。许多旧想法仍然相关,但它们似乎越来越多地解释了为什么明显的坏主意是坏的。(同样,现代读者很难理解弗洛伊德关于潜意识的思想是多么具有革命性,因为它现在是传统智慧。)
Stroustrup just came out with a textbook, Programming: Principles and Practice Using C++. I bought it because I haven't yet failed to learn good stuff from a book of Stroustrup's, but haven't gotten past the first few chapters. So far, all I can say is that I approve of the way he's starting out, and it's at least a good introduction to how C++ should be used.
Stroustrup 刚刚出版了一本教科书,编程:使用 C++ 的原则和实践。我买它是因为我还没有从 Stroustrup 的书中学到好东西,但还没有读完前几章。到目前为止,我只能说我赞同他开始的方式,这至少是对如何使用 C++ 的一个很好的介绍。
回答by Pavel Minaev
While working on the project I am presently involved with, there's a lot of C++ code which has evolved over a significant period of time (over 10 years now). The evolution you speak of is clearly visible there: the older code is often "C with classes" - raw pointers, char*
strings and use of associated C functions, arrays etc; newer code uses ATL smart pointers and such to manage resources, but still sticks to hand-coded loops most of the time, and iterator is a rare sight; and the newest one is chock-full of STL containers, algorithms, shared_ptr
(including custom deleters to manage handles etc), heavily genericized function and class templates, and so on. Most traditional "C with classes" coding techniques, such as raw unencapsulated pointers with manual lifetime management, are very much frowned upon in code reviews these days. Judging by this, it seems that your observation is accurate.
在我目前参与的项目中工作时,有很多 C++ 代码在很长一段时间内(现在已经超过 10 年)得到了发展。您所说的演变在那里清晰可见:旧代码通常是“带有类的 C”——原始指针、char*
字符串以及相关 C 函数、数组等的使用;较新的代码使用 ATL 智能指针等来管理资源,但大部分时间仍然坚持手动编码循环,并且迭代器很少见;最新的一个充满了 STL 容器、算法、shared_ptr
(包括用于管理句柄等的自定义删除器)、高度泛化的函数和类模板等。大多数传统的“带有类的 C”编码技术,例如带有手动生命周期管理的原始未封装指针,如今在代码中非常不受欢迎。如此看来,你的观察似乎是准确的。
The most recent development seems to be a fad for C++0x lambdas - which has a positive side in that it also tilts the balance in favor of using standard algorithms over hand-coded loops, since now you can have all your code inline with algorithms as well.
最近的发展似乎是 C++0x lambdas 的一种时尚——它有一个积极的一面,它也倾斜了平衡,有利于使用标准算法而不是手工编码的循环,因为现在你可以让你的所有代码与算法也是如此。
回答by Nemanja Trifunovic
In my experience it vastly depends on the age of the software product/project. Most new projects that I am aware of do use modern C++ (RAII, STL, Boost). However, there are many C++ projects that are more than 10 years old, and you don't see modern C++ there.
根据我的经验,这在很大程度上取决于软件产品/项目的年龄。我所知道的大多数新项目都使用现代 C++(RAII、STL、Boost)。但是,有许多 C++ 项目已有 10 多年的历史,您在那里看不到现代 C++。
Also, keep in mind that some of the most popular STL implementations were pretty much broken until maybe 5 years ago (MSVC < 7.0 and GNU < 3.00)
另外,请记住,一些最流行的 STL 实现可能在 5 年前就已经被破坏了(MSVC < 7.0 和 GNU < 3.00)
回答by Johan Kotlinski
I wouldn't say that std::vector qualifies as "modern" these days. It is really basic.
这些天我不会说 std::vector 有资格成为“现代”。这真的很基本。
Generally my impression is that people have gained some experience with modern C++ style and sobered up a little. Just to take a simple example, STL for_each was interesting but in practice it does not add a terrible lot of value over a plain C loop. It is harder to debug and sometimes does not provide the best performance. Also the constructs for functional programming in current STL are generally very cumbersome, especially if you got experience from a real functional language like ML.
总的来说,我的印象是人们已经获得了一些现代 C++ 风格的经验并清醒了一点。举一个简单的例子,STL for_each 很有趣,但实际上它并没有比普通的 C 循环增加太多的价值。调试更难,有时不能提供最佳性能。此外,当前 STL 中函数式编程的构造通常非常繁琐,尤其是如果您从真正的函数式语言(如 ML)中获得经验的话。
回答by Diego Sevilla
In my experience (Spanish University), unfortunately, the norm is to not to consider languages in itself. They use the easiest languages to teach programming (i.e. Java), because it is supposed to be easy for teachers and students, and then they use C for the OS classes and such.
不幸的是,根据我的经验(西班牙大学),通常不考虑语言本身。他们使用最简单的语言来教授编程(即Java),因为它对教师和学生来说应该是容易的,然后他们使用C 来教授操作系统课程等等。
C++ is introduced very slightly (at any rate at any course), just to provide a C with classes. They don't get into boost or even STL. I think keeping up with all the characteristics and way of thinking of C++ is costly for both teachers and students. How many of C++ programmers here know enough of all the Boost libraries to use them to give a better solution or to design it? One has to have an interest in keeping up with all the new libraries and idioms.
C++ 的介绍非常少(无论如何在任何课程中),只是为了提供带有类的 C。他们不会进入 boost 甚至 STL。我认为跟上 C++ 的所有特征和思维方式对教师和学生来说都是昂贵的。这里有多少 C++ 程序员对所有 Boost 库足够了解以使用它们来提供更好的解决方案或设计它?人们必须有兴趣跟上所有新的库和习语。
However, as I said, it seems that programming in general (and programming languages in particular) are not taken too seriously, as it seems to be a temporal assignment when they start a job, then forget how to program as they go up in the enterprise tree. Many enterprises here, and the University itself, have the feel that programming can be done by anybody.
然而,正如我所说,似乎一般的编程(尤其是编程语言)并没有被认真对待,因为当他们开始工作时,这似乎是一个时间分配,然后在他们上升时忘记了如何编程。企业树。这里的许多企业,以及大学本身,都觉得编程可以由任何人完成。
If you follow this philosophy, then for most people I know, C++ will always be "C with classes".
如果你遵循这个哲学,那么对于我认识的大多数人来说,C++ 将永远是“带有类的 C”。
Regards,
问候,