选择initializer_list迭代器定义 [英] choice of initializer_list iterator definition

查看:48
本文介绍了选择initializer_list迭代器定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么将 std :: initializer_list< T> :: iterator 定义为裸指针 T const * 而不是 random_access_iterator 概念?

Why std::initializer_list<T>::iterator is defined as a naked pointer T const* rather than a random_access_iterator concept ?

推荐答案

原因可能是来自标准中的小提示

The reason is probably from this little tid-bit from the standard

第18.9节

类型为initializer_list的对象提供对以下对象的数组的访问类型为const E的对象.[注意:一对指针或一个指针加长度将是initializer_list的明显表示形式.initializer_list用于实现指定的初始化列表在8.5.4中.复制初始化器列表不会复制基础元素.—尾注]

An object of type initializer_list provides access to an array of objects of type const E. [ Note: A pair of pointers or a pointer plus a length would be obvious representations for initializer_list. initializer_list is used to implement initializer lists as specified in 8.5.4. Copying an initializer list does not copy the underlying elements. —end note ]

这表明复制时的initializer_lists将具有指针语义而不是值语义,这可能是为什么其迭代器直接是指针并且没有包装为"random-access-iterator"类型的原因.

This indicates that initializer_lists when copied will have pointer semantics and not value semantics, which is probably why their iterators are directly pointers and not wrapped into a "random-access-iterator" type.

AFAIK这是唯一具有指针语义而不是值语义的标准"容器"(请注意此处的引号).

AFAIK this is the only standard "container" (notice the quotes here), to have pointer semantics as opposed to value semantics.

这篇关于选择initializer_list迭代器定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆