现代 C++ 编程指南_MSDN

现代 C++ 编程指南_MSDN
Welcome Back to C++ (Modern C++)

地址:http://msdn.microsoft.com/zh-cn/library/hh279654(v=vs.110).aspx

//========================================
翻译:微软MSDN机器翻译
个人觉得有些地方翻译不是很到位,建议看原文。
个人看MSDN的英语水平还是有的
//========================================

C++ is one of the most widely used programming languages in the world. Well-written C++ programs are fast and efficient. The language is more flexible than other languages because you can use it to create a wide range of apps—from fun and exciting games, to high-performance scientific software, to device drivers, embedded programs, and Windows client apps. For more than 20 years, C++ has been used to solve problems like these and many others, But you already know all that. What you might not know is that an increasing number of C++ programmers have folded up the dowdy C-style programming of yesterday and have donned modern C++ instead.
C++ 是一种最常用编程语言在世界上。 正确编写的 C++ 程序是快速、高效。 ,因为您可以使用它创建广泛 apps 从有趣和激动人心的游戏,为高性能科学软件,对设备驱动程序、嵌入程序和 windows 客户端 apps,语言比其他语言灵活。 超过 20 中, C++ 用于解决此类和许多其他的问题,但是,您已经了解所有+那。 您可能不知道是增加号码 C++ 程序员折叠已过时 C 编程 " 和穿上的现代 C++。

One of the original requirements for C++ was backward compatibility with the C language. Since then, C++ has evolved through several iterations—C with Classes, then the original C++ language specification, and then the many subsequent enhancements. Because of this heritage, C++ is often referred to as a multi-paradigm programming language. In C++, you can do purely procedural C-style programming that involves raw pointers, arrays, null-terminated character strings, custom data structures, and other features that may enable great performance but can also spawn bugs and complexity. Because C-style programming is fraught with perils like these, one of the founding goals for C++ was to make programs both type-safe and easier to write, extend, and maintain. Early on, C++ embraced programming paradigms such as object-oriented programming. Over the years, features have been added to the language, together with highly-tested standard libraries of data structures and algorithms. It's these additions that have made the modern C++ style possible.
一个 C++ 的原始要求与 C 语言的向后兼容性。 从那时, C++ 通过使用类,然后基元 C++ 语言规范的多个迭代 C 发展,然后许多后续增强功能。 因此遗产, C++ 通常称为多范例编程语言。 在 C++ 中,可以执行大多数纯程序 C 编程这涉及的原始指针、数组、 null 终止的字符串)、自定义数据结构和可以启用对性能的其他功能,但也会产生 bug 和复杂。 因为 C 编程填充这样的危险,其中一个 C++ 生成的目标是使程序类型安全、更易于编写,扩展,并维护。 在早期, C++ 接受的编程示例例如面向对象的编程。 多年来,函数已添加到语言,具有高测试的标准数据库结构和算法。 它是呈现现代 C++ 样式成为可能的这些新增。

Modern C++ emphasizes:
Stack-based scope instead of heap or static global scope.
Auto type inference instead of explicit type names.
Smart pointers instead of raw pointers.
std::string and std::wstring types instead of raw char[] arrays.
Standard template library (STL) containers—for example, vector, list, and map—instead of raw arrays or custom containers.
STL algorithms instead of manually coded ones.
Exceptions, to report and handle error conditions.
Inline lambda functions instead of small functions implemented separately.
The C++ language itself has also evolved. Compare the following code snippets. This one shows how things used to be in C++:
现代 C++ 基础:
而不是堆或静态全局范围的基于堆栈的大小。
而不是显式类型名称的自动类型推理。
而不是原始指针的智能指针。
std::string 和 std::wstring 键入而不是原始的 char[] 数组。
而是标准模板库 (STL) (stl) 容器 (例如、 vector、 list和 map—原始数组或自定义容器。
而不是手动编码的那些的 STL 算法。
异常,对报告和处理错误条件。
内联 lambda 函数而不是单独实现的小的函数。
C++ 语言也发展。 比较下面的代码段。 这是一个演示了如何使用 C++ 中:

 1 //===========================================
 2 circle* p = new circle( 42 );
 3 vector<shape*> v = load_shapes();
 4 
 5 for( vector<circle*>::iterator i = v.begin(); i != v.end(); ++i ) {
 6     if( *i && **i == *p )
 7         cout << **i << “ is a match\n”;
 8 }
 9 
10 for( vector<circle*>::iterator i = v.begin();
11         i != v.end(); ++i ) {
12     delete *i; // not exception safe
13 }
14 
15 delete p;
16 //===========================================

Here's how the same thing is accomplished in modern C++:
这是同一事物如何在现代 C++ 完成:

1 //===========================================
2 auto p = make_shared<circle>( 42 );
3 vector<shared_ptr<shape>> v = load_shapes();
4 
5 for_each( begin(v), end(v), [&]( const shared_ptr<shape>& s ) {
6     if( s && *s == *p )
7         cout << *s << " is a match\n";
8 } );
9 //===========================================

 

In modern C++, you don't have to use new/delete or explicit exception handling because you can use smart pointers instead. When you use the auto type deduction and lambda function, you can write code quicker, tighten it, and understand it better. And for_each is cleaner, easier to use, and less prone to unintended errors than a for loop. You can use boilerplate together with minimal lines of code to write your app. And you can make that code exception-safe and memory-safe, and have no allocation/deallocation or error codes to deal with.
在现代 C++,,因为您可以使用智能指针,不必使用 new/delete 或显式异常处理。 当您使用 auto 类型推导和 lambda 函数时,可以编写代码更快,生成并更好地了解它。 并 for_each 比 for 循环是更加整洁,更易于使用,并且不容易意外错误。 可以与代码一起最小行使用的编写自己的应用程序。 您可以对内存安全代码异常安全,并没有分配或释放或错误代码。

Modern C++ incorporates two kinds of polymorphism: compile-time, through templates, and run-time, through inheritance and virtualization. You can mix the two kinds of polymorphism to great effect. For example, the STL template shared_ptr uses internal virtual methods to accomplish its apparently effortless type erasure. But don't over-use virtualization for polymorphism when a template is the better choice. Templates can be very powerful.
现代 C++ 合并两个多态性:编译时,通过模板和运行时,通过继承和虚拟化。 可以混合使用这两个多态性到大的影响。 例如, STL 模板 shared_ptr 使用内部虚方法完成其清单未费力的类型抹除。 ,该模板是更好的选择时,但是,不要过度使用多态性的虚拟化。 模板可以非常强大。

If you're coming to C++ from another language, especially from a managed language in which most of the types are reference types and very few are value types, know that C++ classes are value types by default. But you can specify them as reference types to enable polymorphic behavior that supports object-oriented programming. A helpful perspective: value types are more about memory and layout control, reference types are more about base classes and virtual functions to support polymorphism. By default, value types are copyable—they each have a copy constructor and a copy assignment operator. When you specify a reference type, make the class non-copyable—disable the copy constructor and copy assignment operator—and use a virtual destructor, which supports the polymorphism. Value types are also about the contents, which, when they are copied, give you two independent values that you can modify separately. But reference types are about identity—what kind of object it is—and for this reason are sometimes referred to as polymorphic types.
如果到达 C++ " 自另一种语言,尤其是从大多数类型引用类型的一种托管语言和极少数是值类型,了解默认情况下 C++ 类是值类型。 但是,您可以指定这些引用启用支持面向对象编程的多态行为的类型。 一个有用的角度:值类型更多有关内存,并窗体控件,引用类型是更多有关基类和虚函数支持多态性。 默认情况下,每个具有复制构造函数和一复制赋值运算符的值类型是 copyable 它们。 当指定引用类型,使类非 copyable 禁用复制构造函数和复制赋值运算符和使用虚拟析构函数,支持多态性。 值类型也是有关目录,,那么,当将其复制时,提供了两个独立的值可以单独地修改。 但是,请引用类型是有关标识哪些有点儿对象是和因此有时称为多态类型。

C++ is experiencing a renaissance because power is king again. Languages like Java and C# are good when programmer productivity is important, but they show their limitations when power and performance are paramount. For high-efficiency and power, especially on hardware that has limited hardware, nothing beats modern C++.
,因为幂同样,是手 C++ 经历新生。 如 Java 的语言和 C# 比较好,当程序员效率很重要时,但是,它们显示其限制,在功能和性能是至高无上时。 对于高效率和功能,特别是在有限的硬件的硬件,没有调用现代 C++。

Not only the language is modern, the development tools are, too. Visual Studio makes all parts of the development cycle robust and efficient. It includes Application Lifecycle Management (ALM) tools, IDE enhancements like IntelliSense, tool-friendly mechanisms like XAML, and building, debugging, and many other tools.
不仅该语言是现代的,开发工具是,还为。 Visual Studio 使所有部件开发周期的更可靠并且有效。 它包括应用程序 (ALM)生命周期管理工具、 IDE 改进与 IntelliSense,工具友好的结构与 XAML 和生成,调试和许多其他工具。

The articles in this part of the documentation provide high-level guidelines and best practices for the most important features and techniques for writing modern C++ programs.
文档中的此部分中的文章为最重要的功能提供高级教程和写入现代 C++ 程序的最佳做法和技术。

 

posted @ 2012-04-13 12:43  天远  阅读(533)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远