3

D 中的容器是否默认具有值或引用语义?如果它们具有引用语义并不会从根本上阻碍 D 中函数式编程风格的使用(与 C++11 的移动语义相比),例如以下(学术)示例:

auto Y = reverse(sort(X));

X容器在哪里。

4

2 回答 2

4

Whether containers have value semantics or reference semantics depends entirely on the container. The only built-in containers are dynamic arrays, static arrays, and associative arrays. Static arrays have strict value semantics, because they sit on the stack. Associative arrays have strict reference semantics. And dynamic arrays mostly have reference semantics. They're elements don't get copied, but they do, so they end up with semantics which are a bit particular. I'd advise reading this article on D arrays for more details.

As for containers which are official but not built-in, the containers in std.container all have reference semantics, and in general, that's how containers should be, because it's highly inefficient to do otherwise. But since anyone can implement their own containers, anyone can create containers which are value types if they want to.

However, like C++, D does not take the route of having algorithms operate on containers, so as far as algorithms go, whether containers have reference or value semantics is pretty much irrelevant. In C++, algorithms operate on iterators, so if you wanted to sort a container, you'd do something like sort(container.begin(), container.end()). In D, they operate on ranges, so you'd do sort(container[]). In neither language would you actually sort a container directly. Whether containers themselves have value or references semantics is therefore irrelevant to your typical algorithm.

However, D does better at functional programming with algorithms than C++ does, because ranges are better suited for it. Iterators have to be passed around in pairs, which doesn't work very well for chaining functions. Ranges, on the other hand, chain quite well, and Phobos takes advantage of this. It's one of its primary design principles that most of its functions operate on ranges to allow you to do in code what you typically end up doing on the unix command line with pipes, where you have a lot of generic tools/functions which generate output which you can pipe/pass to other tools/functions to operate on, allowing you to chain independent operations to do something specific to your needs rather than relying on someone to have written a program/function which did exactly what you want directly. Walter Bright discussed it recently in this article.

So, in D, it's easy to do something like:

auto stuff = sort(array(take(map!"a % 1000"(rndGen()), 100)));

or if you prefer UFCS (Universal Function Call Syntax):

auto stuff = rndGen().map!"a % 1000"().take(100).array().sort();

In either case, it generates a sorted list of 100 random numbers between 0 and 1000, and the code is in a functional style, which C++ would have a much harder time doing, and libraries which operate on containers rather than iterators or ranges would have an even harder time doing.

于 2012-10-31T18:34:39.193 回答
2

内置容器

D 中唯一的内置容器是切片(也称为数组/动态数组)和静态数组。后者具有值语义(与 C 和 C++ 不同) - 整个数组在传递时被(浅)复制。

至于切片,它们是具有间接性的值类型,因此您可以说它们同时具有值​​和引用语义。

想象T[]struct这样:

struct Slice(T)
{
    size_t length;
    T* ptr;
}

whereptr是指向切片第一个元素的指针,并且length是切片边界内的元素数。您可以访问切片的.ptrand.length字段,但虽然数据结构与上述相同,但它实际上是内置编译器,因此未在任何地方定义(名称Slice仅用于说明目的)。

知道了这一点,您可以看到复制切片(分配给另一个变量,传递给函数等)只是复制一个长度(无间接 - 值语义)和一个指针(具有间接 - 引用语义)。

换句话说,切片是数组的视图(位于内存中的任何位置),并且同一个数组可以有多个视图。

算法

sortreversestd.algorithm就地工作来满足尽可能多的用户。如果用户想要将结果放入切片的 GC 分配副本中并保持原始不变,则可以轻松完成 ( X.dup)。如果用户想将结果放在自定义分配的缓冲区中,也可以这样做。最后,如果用户想要就地排序,这是一个选项。无论如何,任何额外的开销都是明确的。

然而,重要的是要注意标准库中的大多数算法不需要变异,而是返回延迟评估的范围结果,这是函数式编程的特征。

用户定义的容器

当涉及到用户定义的容器时,它们可以有任何他们想要的语义——在 D 中任何配置都是可能的。

中的容器是具有复制方法的std.container引用类型,因此稍微模拟了切片。.dup

于 2012-10-31T16:06:06.087 回答