masterminds of programming 翻译 (四)

来源:互联网 发布:小程序模板源码 编辑:程序博客网 时间:2024/05/16 00:44

What do you mean by “value semantics” and “general resource management”?

您刚才说的值语义和综合资源管理是指什么?

 

Bjarne: “Value semantics” is commonly used to refer to classes where the objects have the property that when you copy one, you get two independent copies (with the same value).

Bjarne:值语义一般来说是针对一个类的对象来说的,它有这样的性质,你拷贝它,你得到两个独立的副本(拥有相同的值)

 

For example:

例如:

 

X x1 = a;

X x2 = x1; // now x1==x2 //现在 x1==x2

x1 = b; // changes x1 but not x2// 改变x1但没有改变x2

// now x1!=x2 ( provided X(a)!=X(b) ) //现在 x1!=x2 (因为X(a)!=X(b))

 

This is of course what we have for usual numeric types, such as ints, doubles, complex numbers, and mathematical abstractions, such as vectors. This is a most useful notion, which C++ supports for built-in types and for any user-defined type for which we want it. This contrast to Java where built-in types such and char and int follow it, but user-defined types do not, and indeed cannot. As in Simula, all user-defined types in Java have reference

semantics. In C++, a programmer can support either, as the desired semantics of a type requires. C# (incompletely) follows C++ in supporting user-defined types with value semantics.

对于普通的数值类型,如整形,浮点,复数,以及数学方面的抽象类,比如向量类(vector)等我们当然都有这样的性质。这是一个很有用的概念,我们想让C++不管在内置类型还是在用户自定义类型上都支持它。Java不一样,它只支持内置类型,如字符类型(char)和整型(int),但不支持也无法支持用户自定义类型。就像在Simula一样,Java里所有的用户自定义类型都是引用语义。在C++里,这两种语义程序员都可以按需使用。C#(不完整地)追随了C++,对用户自定义类型给予值语义支持。

 

General resource management” refers to the popular technique of having a resource (e.g., a file handle or a lock) owned by an object. If that object is a scoped variable, the lifetime of the variable puts a maximum limit on the time the resource is held. Typically, a constructor acquires the resource and the destructor releases it. This is often called RAII (Resource Acquisition Is Initialization) and integrates beautifully with error handling using exceptions. Obviously, not every resource can be handled in this way, but many can, and for those, resource management becomes implicit and efficient.

“综合资源管理”是指管理对象拥有的资源(例如一个文件句柄或者是一个锁)的技术。如果那个对象是一个有作用域的变量,那么它的生命周期就对它能够拥有资源的最长时间给予了限制。典型来说,构造器获得资源而析构器释放资源。这经常被称作RAII(资源获取即初始化)也和使用异常的错误处理机制完美整合在一起。显然,并不是对每一个资源都可以这样处理,但很多可以,对于可以的,资源管理技术显得含蓄且有效。

 

Close to the hardware” seems to be a guiding principle in designing C++. Is it fair to say that C++ was designed more bottom-up than many languages, which are designed topdown, in the sense that they try to provide abstractly rational constructs and force the compiler to fit these constructs to the available computing environment?

“接近硬件”看上去好像是设计C++的指导纲领。很多语言试图提供抽象的合理构造体并强制编译器去让它们适应可用的计算环境,从这个意义上来讲,说C++是自下而上设计出来,而不是像那些语言那样是自上而下的,你觉得这么说公平吗?

 

Bjarne: I think top-down and bottom-up are the wrong way to characterize those design decisions. In the context of C++ and other languages, “close to the hardware” means that the model of computation is that of the computer—sequences of objects in memory and operations as defined on objects of fixed size—rather than some mathematical abstraction. This is true for both C++ and Java, but not for functional languages. C++ differs from Java in that its underlying machine is the real machine rather than a single abstract machine.

Bjarne:我觉得用自上而下和自下而上来描述这些设计决定的特性是错误的。就C++和其它语言而言,“接近硬件”意味着计算模型是属于特定的机器的,即内存中的对象序列和定义在固定大小的对象中的操作,而不是一些数学上的抽象。C++Java都是这样的,但函数语言不是。C++Java的不同在于前者建立在真实的机器上而后者是在抽象的机器上。

 

The real problem is how to get from the human conception of problems and solutions to the machine’s limited world. You can “ignore” the human concerns and end up with machine code (or the glorified machine code that is bad C code). You can ignore the machine and come up with a beautiful abstraction that can do anything at extraordinary cost and/or lack of intellectual rigor. C++ is an attempt to give a very direct access to hardware when you need it (e.g., pointers and arrays) while providing extensive abstraction mechanisms to allow high-level ideas to be expressed (e.g., class hierarchies and templates).  That said, there has been a consistent concern for runtime and space performance throughout the development of C++ and its libraries. This pervades both the basic language facilities and the abstraction facilities in ways that are not shared by all languages.

真正的问题是如何把人对于问题和解决方法的构想引入有限的机器世界。你可以“忽略”对于人的考虑而选择机器代码(或者那些美其名曰机器代码的糟糕的C代码)。你也可以忽略对机器的考虑而选择一个优美的以极高代价或且欠缺严谨但可以解决任何问题的抽象体。C++在你需要时给你提供对硬件的直接访问并且也提供大量的抽象机制使得高级思想可以得到表达,这是C++的一个尝试。另外,在开发C++和相关库时对运行时间和空间的性能考虑贯穿始终。这种思想遍及基础语言工具和抽象工具,不是所有语言都是这样的。

原创粉丝点击