早在 2005 年,我在一家大部分代码都用 C++ 编写的公司工作。2006 年,管理层决定用 C# 开发新项目,并把一些 C++ 项目迁移到 C#。他们反对 C++ 的理由是:
- C++ 太复杂了。
- C++ 的构建系统太难管理和维护。
- C++ 停滞了许多年,正在走向消亡。
- 招聘 C++ 开发者变得非常困难。
当然,网络上还有许多其他观点,都在劝管理者不要继续用 C++ 开发。
More important than security is the fact that C and C++ are simply too hard to program, compared to any safe language. Not only is it too easy to write a catastrophic bug in C/C++, it is even easier to write more ordinary bugs. And these bugs are very often not caught by the compiler; they are discovered months or years later, in production, often after the programmer who wrote them has moved on.As a multi-paradigm system programming language, C++ is a complete beast when it comes to its feature-set and its specs, followed up with a steep learning curve and a very long path to mastery. It’s challenging to keep track of all its complex syntactical constructs that more often than not seem confusing and ambiguous.C++ does not have a proper module system that most modern languages provide. D, Java, C#, Python, Clojure, you name it. All of them got one. In combination with its primitive preprocessor, this usually results in a messy structure of header files and implementation files that rely on include guards to give basic protection against multiple inclusion.
当时,作为 C++ 开发者,我们没有多少有力的论据能说服他们不要放弃 C++,尤其是当时微软正在大力推广 .NET 而非 C++,.NET 技术的声势如日中天。
如今已是 2018 年,C++ 语言发生了许多变化。问题是:如果您的公司决定弃用 C++,您可以用哪些论据说服管理者继续用它?
首先,假装现在一切都很好、C++ 已经克服了所有弱点,那就错了。如果我们想改变“C++ 已经变得只适合专家”这种印象,还有许多地方需要改进。
幸运的是,让 C++ 更易用正是 C++ 委员会的目标之一,正如 Herb Sutter 在最近一次采访中所解释的:
Since mid-2015 in particular, I decided to focus my work on seeing if I could find ways to make C++ 程序 simpler. Because C++ has a long-term future, anything we can do to make C++ programs easier to write, read, and maintain will deliver big benefits to the whole industry.
The way I went about it was by systematically looking at C++ code and seeing where all the boilerplate is – that is, find the things that C++ programmers already do all the time, but that they are forced to express indirectly with excessive ceremony or in complex and brittle ways, and see if we can find a very small number of general mechanisms that could let them express those things in a simpler and more direct and robust way. If we can find the places where we regularly have to fight the language (such as the language’s defaults) or work around the language (such as by using macros or proprietary language extensions) or just rely on English advice (Scott Meyers’ books are great, but wouldn’t it be great if we didn’t need to say 75% of what’s in them), and in those places be able to directly state what we want, that would be actively improving those things that we are doing already.
A “small” historical example is the range-based for loop: We could already write for loops that visit each element, but it took more ceremony such as writing the incrementing logic by hand, and it had more ways to go wrong such as inadvertently incrementing the index or iterator in the body of the loop and skipping elements. By having a ranged-based for in the language, we elevated that very common coding pattern into a language feature that lets us now say directly what we want to do, namely visit each element in order, and that is obviously correct by construction just by looking at the first line of the loop without looking inside the body to see if it might skip elements or something. And I think the range-based for loop, though a “small” feature, was one of the major successes of C++11 – an improvement that people now use every day. Adding it to the language made the language (slightly) bigger, but it made C++ programs simpler.
- 支持 C++ 的第一个论据是:自 2011 年以来,许多特性被加入语言以改进它,更多让 C++ 更易用的特性也在路上。为了让论证更有说服力,最好展示一些来自现代 C++ 库和项目的代码片段。

- C++ 不再走在消亡的路上;许多编程语言排行榜指标都显示 C++ 正在进步。

- C++ 在开发者论坛上依然非常活跃。只要看看 Stack Overflow 就知道,每天都有许多 C++ 问题被发布。

- C++ 仍被广泛用于新项目开发,这篇关于 GitHub 语言趋势的有趣文章中的 GitHub 排名就说明了这一点。

- C++ 社区正在壮大。看看 Google+、LinkedIn、Facebook 或 Twitter,您会发现对 C++ 感兴趣的用户越来越多。
总而言之,C++ 已经回归并且比以往更强大,这不仅仅是主观看法——统计数据本身就是证明。今天,保留您现有 C++ 代码的论据比比皆是。在许多情况下,决定权并不在开发者手中,但开发者可以找出最契合自身具体情况的论据,用它们来说服管理者继续把 C++ 作为主要编程语言。
