模板类std::iterator
被设置为在C++ 17中被弃用。为什么这样?确保std::iterator_traits
有效,特别是在可以使用默认模板参数的情况下,它是一种非常方便的方式。有没有其他方式在C++ 17中做到这一点?std :: iterator为什么不推荐使用?
回答
从the proposal that suggested its deprecation:
为帮助编写迭代器类,原来的标准库提供的迭代器类模板自动五大类型定义预计iterator_traits每个迭代的声明。然后将其在库本身使用,例如在
std::ostream_iterator
规格:template <class T, class charT = char, class traits = char_traits<charT> > class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void>;
的
void
参数的长序列是少得多清楚地向读者不是简单的类定义本身,提供预期的typedef这是当前工作草案采用的方法,遵循C++ 14中设置的模式,其中我们不推荐遍及函数库unary_function
和binary_function
的推导。除了清晰度降低之外,迭代器模板还为粗心大意设置了一个陷阱,就像在典型的用法中它将是一个依赖的基类一样,这意味着它不会在类内进行名称查找期间查找或其成员职能。这导致吃惊用户试图理解为什么下面简单的用法不工作:
#include <iterator> template <typename T> struct MyIterator : std::iterator<std::random_access_iterator_tag, T> { value_type data; // Error: value_type is not found by name lookup // ... implementations details elided ... };
单独清晰的理由足以说服LWG到标准库规范更新不再授权标准的迭代器adapators为来自
std::iterator
,所以在标准本身内不再使用该模板。因此,它看起来像是一个强大的弃用对象。
您还可以在LWG 2438中看到STL的推理。 (h/t T.C.)
至于其他一些做法,并不是真的。你基本上可以实现你自己的版本std::iterator
(这不是太难)或手动写出所有这些typedef(这也不是太困难,而我实际上更喜欢它)。
STL在[LWG 2438](https://timsong-cpp.github.io/lwg-issues/2438)中的论点似乎也很合理:该名称可能误导用户(特别是来自* ahem *某些其他编程语言)认为派生是强制性的,或者写一个接受'std :: iterator'的函数是有意义的。 –
- 1. 为什么不推荐使用std :: strstream?
- 2. 为什么Logger.isInfoEnabled不推荐使用org.jboss.logging.Logger?
- 3. 为什么不推荐使用isJavaLetterOrDigit?
- 4. 为什么不推荐使用JButton.enable?
- 5. 为什么不推荐使用struts2 FilterDispatcher?
- 6. 为什么SET不推荐使用?
- 7. 为什么不推荐使用StringTokenizer?
- 8. Object.observe()为什么不推荐使用
- 9. 为什么不推荐使用body.scrollTop?
- 10. 为什么不推荐HibernateTemplate?
- 11. 为什么不推荐使用window.showModalDialog?代替使用什么?
- 12. 为什么不推荐使用assert_template,而应该使用什么?
- 13. 为什么Visual Studio使用std :: iterator <>而不是mine :: iterator <>
- 14. jQuery切换不推荐使用什么?
- 15. 为什么gunicorn_django不再被推荐?
- 16. 为什么不通过推荐工作?
- 17. 为什么不再推荐mysql扩展?
- 18. 什么是C++ 11标准的等价物不推荐使用std :: auto_ptr?
- 19. 为什么std :: count(_if)返回iterator :: difference_type而不是size_t?
- 20. 为什么不推荐使用xmp HTML标记?
- 21. 为什么在Rails中不推荐使用auto_link?
- 22. 为什么不推荐使用hibernate org.hibernate.classic.Validatable接口?
- 23. 为什么在Python中不推荐使用MutableString?
- 24. 为什么UIAlertView如果不推荐使用iOS 8?
- 25. 为什么在PHP中不推荐使用`ereg`?
- 26. 为什么不推荐使用hibernate的ClassMetadata.getIdentifier(Object,EntityMode)
- 27. 为什么在JAVA中不推荐使用默认包?
- 28. 为什么在Django 1.5+中不推荐使用markdown?
- 29. 为什么不推荐使用[DataMember(EmitDefaultValue = false)]?
- 30. 为什么不推荐在SQL中使用“LIKE”?
是的,在C++ 17中做同样的事情有一个方便的方法:你只是做同样的事情。 “已弃用”意味着只有**,它**可能会在未来消失。这并不意味着你不能使用它,或者它不会像往常一样做。 –
@PeteBecker:但问题是,如果它被弃用,那么为什么程序员会在下一个版本左右的时候使用它?为什么它开始使用,如果它非常方便? – Nawaz
@Nawaz - 这个问题倒退了。如果它在标准中,为什么不使用它? **标准中的任何**可能会在未来消失。是的,不赞成使用的东西可能会消失,或者可能不会;考虑一下C头文件,这些头文件在C++中不推荐使用,但实际上,它们永远不会消失。 –