STL迭代器中的value_type(const Iterator&)函数为什么返回指针?

电子科学 2016-07-25 10:34:57

template <class Iterator>
inline typename iterator_traits<Iterator>::value_type*
vlue_type(const Iterator&){
return static_cast<typename iterator_traits<Iterator>::value_type*>(0);


为什么萃取器返回指针?谢谢各位
...全文
400 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
电子科学 2016-07-27
  • 打赏
  • 举报
回复
感谢@iyomumx
iyomumx 2016-07-26
  • 打赏
  • 举报
回复
STL 中没有这个函数,请给出具体的上下文
iyomumx 2016-07-26
  • 打赏
  • 举报
回复 1
这段代码出现在 SGI STL 的 stl_iterator_base.h 文件里,其中有注释:
引用
// The overloaded functions iterator_category, distance_type, and // value_type are not part of the C++ standard. (They have been // replaced by struct iterator_traits.) They are included for // backward compatibility with the HP STL. // We introduce internal names for these functions.
返回指针的原因有很多,比如 value_type 禁止拷贝、或者不能默认构造、又或者构造一个需要大量开销,返回指针只需要解引用即可得到原类型。
电子科学 2016-07-26
  • 打赏
  • 举报
回复
这个是在《STL源码剖析》里面第三章的内容,函数意思是返回形参的value_type类型
赵4老师 2016-07-25
  • 打赏
  • 举报
回复
因为引用是用指针实现的?

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧